package com.huyi.service.invoice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huyi.service.base.entity.ZcInvoiceInf;
import com.huyi.service.base.service.IZcInvoiceInfService;
import com.huyi.service.invoice.service.OwnInvoiceService;
import com.keao.tianhu.starter.mybatis.plus.entity.QueryRequest;
import com.tianhu.common.core.constant.SalaryConstants;
import com.tianhu.common.core.domain.R;
import com.tianhu.common.core.web.controller.BaseController;
import com.tianhu.common.core.web.domain.AjaxResult;
import com.tianhu.common.security.annotation.PreAuthorize;
import com.tianhu.common.security.service.TokenService;
import com.tianhu.system.api.domain.SysUser;
import com.tianhu.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 发票信息
 */
@RestController
@RequestMapping("/ownInvoice")
public class OwnInvoiceController extends BaseController {
    @Autowired
    private TokenService tokenService;
    //自写发票服务类
    @Autowired
    private OwnInvoiceService   ownInvoiceService;
    //自动生成发票服务类
    @Autowired
    private IZcInvoiceInfService zcInvoiceInfService;




    /**
     * 查询发票列表
     * @param ziiNo
     * @param ziiPurchaserNo
     * @param ziiSellerNo
     * @param ziiDate
     * @param ziiType
     * @param request
     * @return
     */
    @PreAuthorize(hasPermi = "invoice:inf:list")
    @GetMapping("/list")
    public R list(
                  @RequestParam(required=false) String ziiNo,
                  @RequestParam(required=false) String ziiPurchaserNo,
                  @RequestParam(required=false) String ziiSellerNo,
                  @RequestParam(required=false) Map ziiDate,
                  @RequestParam(required=false) String ziiType,
                  @RequestParam(required=false) String ziiPurchaserName,
                  @RequestParam(required=false) String ziiSellerName,
                  QueryRequest request) {
        //查询当前操作员
        LoginUser userInfo = tokenService.getLoginUser();
        SysUser use = userInfo.getSysUser();
        //获取企业
        String companyId = use.getCompanyId();
        Map map = new HashMap();
        if (!SalaryConstants.OPEX.equals(companyId)) {
            map.put("ziiCompanyId", companyId);
        }
        //发票编号
        map.put("ziiNo", ziiNo);
        //购方识别码
        map.put("ziiPurchaserNo", ziiPurchaserNo);
        //销方识别码
        map.put("ziiSellerNo", ziiSellerNo);
        //开票日期
        map.put("startTime",ziiDate.get("ziiDate[0]"));
        map.put("endTime",ziiDate.get("ziiDate[1]"));
        //开票类型
        map.put("ziiType", ziiType);
        //购方
        map.put("ziiPurchaserName",ziiPurchaserName);
        //销方
        map.put("ziiSellerName",ziiSellerName);
        IPage<Map> list = ownInvoiceService.selectInvoiceInfList(map,request);
        return R.ok(list);
    }


    /**
     * 获取发票明细信息
     */
    @PreAuthorize(hasPermi = "invoice:invoice:query")
    @GetMapping(value = "/{ziiId}")
    public AjaxResult getContractInfo(@PathVariable("ziiId") String ziiId, QueryRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        //获取用户信息
        LoginUser userInfo = tokenService.getLoginUser();
        //企业id
        String companyId = userInfo.getSysUser().getCompanyId();
        LambdaQueryWrapper<ZcInvoiceInf> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //公司编号
        lambdaQueryWrapper.eq(ZcInvoiceInf::getZiiCompanyId,companyId);
        //发票id
        lambdaQueryWrapper.eq(ZcInvoiceInf::getZiiId,ziiId);
        List<ZcInvoiceInf>  invoiceInfList =   zcInvoiceInfService.findZcInvoiceInfs(lambdaQueryWrapper);
        return AjaxResult.success(invoiceInfList);
    }
}
