package com.yh.saas.plugin.yiliangyiyun.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.yh.saas.plugin.yiliangyiyun.entity.ContractManagementInfo;
import com.yh.saas.plugin.yiliangyiyun.entity.PaymentManagement;
import com.yh.saas.plugin.yiliangyiyun.entity.view.PaymentView;
import com.yh.saas.plugin.yiliangyiyun.service.IIdentityAuthenticationInfoService;
import com.yh.saas.plugin.yiliangyiyun.service.IPaymentManagementService;
import com.yh.saas.plugin.yiliangyiyun.util.XmlUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 付款管理 前端控制器
 * </p>
 *
 * @author Gdc
 * @since 2021-09-06
 */
@RestController
@RequestMapping("/paymentManagement")
public class PaymentManagementController {

    @Autowired
    private XmlUtil xmlUtil ;
    @Autowired
    private IPaymentManagementService paymentManagementService;
    @Autowired
    private IIdentityAuthenticationInfoService iIdentityAuthenticationInfoService;
//    @Autowired
//    private XmlUtil xmlUtil;
    /**
     * 付款管理列表
     * @param paymentManagement
     * @return
     */
    @GetMapping("/selectPaymentManagement")
    public Page<PaymentManagement> selectPaymentManagement(PaymentManagement paymentManagement){
        return paymentManagementService.selectPaymentManagement(paymentManagement);
    }
    /**
     * 查看
     */
    @GetMapping("/getInfo")
    public PaymentManagement getInfo(@RequestParam String id) {
        return paymentManagementService.getInfo(id);
    }
    /**
     * 付款
     * @param paymentManagement
     * @return
     */
    @PostMapping("/api/payMoney")
    public String payMoney(@RequestBody PaymentManagement paymentManagement){
        return paymentManagementService.payMoney(paymentManagement);
    }
    /**
     * 合同编号下拉列表
     * @param compId
     * @return
     */
    @GetMapping("/selectContractNoList")
    public List<ContractManagementInfo> selectContractNoList(String compId) {
        return paymentManagementService.selectContractNoList(compId);
    }
    /**
     * 收款
     * @param paymentManagement
     * @return
     */
    @PostMapping("/api/collectMoney")
    public String collectMoney(@RequestBody PaymentManagement paymentManagement){
        return paymentManagementService.collectMoney(paymentManagement);
    }
    /**
     * 编辑应收
     * @param paymentManagement
     * @return
     */
    @PostMapping("/api/editCollect")
    public String editCollect(@RequestBody PaymentManagement paymentManagement){
        return paymentManagementService.editCollect(paymentManagement);
    }
    /**
     * 备注
     * @param paymentManagement
     * @return
     */
    @PostMapping("/api/addRemarks")
    public String addRemarks(@RequestBody PaymentManagement paymentManagement){
        return paymentManagementService.addRemarks(paymentManagement);
    }

    /**
     * 结算
     * @param paymentManagement
     * @return
     */
    @PostMapping("/api/editInfo")
    public String editInfo(@RequestBody PaymentManagement paymentManagement){
        return paymentManagementService.editInfo(paymentManagement);
    }
    /**
     * 反结算
     * @param paymentManagement
     * @return
     */
    @PostMapping("/api/ReSettlement")
    public String ReSettlement(@RequestBody PaymentManagement paymentManagement){
        return paymentManagementService.ReSettlement(paymentManagement);
    }
    /**
     * 删除
     */
    @PostMapping("/api/deleteInfo")
    public void deleteInfo(@RequestBody PaymentManagement paymentManagement){
        paymentManagementService.deleteInfo(paymentManagement.getId());
    }
    /**
     * 查累积量
     * @param paymentManagement
     * @return
     */
    @GetMapping("/cumulant")
    public Float cumulant(PaymentManagement paymentManagement) {
        return paymentManagementService.cumulant(paymentManagement);
    }
    /**
     * 结算开启审核流
     * @param paymentManagement
     * @return
     */
    @PostMapping("/api/openAuditFlow")
    public  String openAuditFlow(@RequestBody PaymentManagement paymentManagement){
        return paymentManagementService.openAuditFlow(paymentManagement);
    }
    /**
     * 查taskId
     * @param paymentManagement
     * @return
     */
    @GetMapping("/selectTaskId")
    public String selectTaskId(PaymentManagement paymentManagement) {
        return paymentManagementService.selectTaskId(paymentManagement);
    }


    /**
     * 发票
     * @param paymentManagement
     * @return
     */
    @PostMapping("/api/readXmlManagement")
    public List<String> readXmlManagement(@RequestBody PaymentManagement paymentManagement) {
        List<PaymentManagement> paymentManagementList = paymentManagement.getPaymentManagementList();
        List<String> str = new ArrayList<>();
        for(int i=0;i<paymentManagementList.size();i++){
            paymentManagement = paymentManagementList.get(i);
            paymentManagement.setInvoicing("1");
            paymentManagement.setIdentityAuthenticationInfo(iIdentityAuthenticationInfoService.selectById(paymentManagement.getIdentityId()));
            paymentManagementService.updateById(paymentManagement);
        }
        if(paymentManagementList.size() >0 && !"1".equals(paymentManagementList.get(0).getInvoiceFlag())){
            List<PaymentManagement> list1 = new ArrayList<>();
            for(int i=0;i<paymentManagementList.size();i++){
                PaymentManagement tmp = paymentManagementList.get(i);
                if(tmp.getAmountIngPayable() > 100000){
                    if(tmp.getPureWeight() == null){
                        tmp.setPureWeight(tmp.getNetWeight());
                    }

                    String regEx = "[^0-9]";
                    Pattern p = Pattern.compile(regEx);
                    Matcher m = p.matcher(tmp.getPaymentNo());
                    String number = m.replaceAll("").trim();
                    //定义纯重和
                    Float weight = tmp.getPureWeight();
                    //定义应付金额和
                    Double payable = tmp.getAmountIngPayable();
                    //第一张发票
                    tmp.setPureWeight(weight/2);
                    tmp.setAmountIngPayable(payable/2);
                    tmp.setOddNumbers(number.substring(4) + "1");
                    if(tmp.getGrossWeight() != null){
                        tmp.setGrossWeightStr(tmp.getGrossWeight() + "");
                    }
                    else{
                        tmp.setGrossWeightStr("");
                    }
                    if(tmp.getTare() != null){
                        tmp.setTareStr(tmp.getTare() + "");
                    }
                    else{
                        tmp.setTareStr("");
                    }
                    tmp.setRemarkss("车号:" + tmp.getCarNo() + "毛:" +
                            tmp.getGrossWeightStr()
                            + " 皮:" +
                            tmp.getTareStr()
                            + " 净:" +
                            tmp.getNetWeight() + " 纯:" + weight + " 应付:" + payable + " 单号:" + m.replaceAll("").trim() + " ,★☆性质:贸易粮 形态:玉米堆（1）"
                    );
                    if(tmp.getGrade() == null){
                        tmp.setGrade(" ");
                    }
                    list1.add(tmp);
                    //第二张发票
                    PaymentManagement tmp1 = new PaymentManagement();
                    BeanUtils.copyProperties(tmp,tmp1);
                    tmp1.setPureWeight(weight - tmp.getPureWeight());
                    tmp1.setAmountIngPayable(payable - tmp.getAmountIngPayable());
                    tmp1.setOddNumbers(number.substring(4) + "2");
                    if(tmp1.getGrossWeight() != null){
                        tmp1.setGrossWeightStr(tmp1.getGrossWeight() + "");
                    }
                    else{
                        tmp1.setGrossWeightStr("");
                    }
                    if(tmp1.getTare() != null){
                        tmp1.setTareStr(tmp1.getTare() + "");
                    }
                    else{
                        tmp1.setTareStr("");
                    }
                    if(tmp1.getGrade() == null){
                        tmp1.setGrade(" ");
                    }
                    tmp1.setRemarkss("车号:" + tmp1.getCarNo() + "毛:" +
                            tmp1.getGrossWeightStr()
                            + " 皮:" +
                            tmp1.getTareStr()
                            + " 净:" + tmp1.getNetWeight() + " 纯:" + weight + " 应付:" + payable + " 单号:" + m.replaceAll("").trim() + " ,★☆性质:贸易粮 形态:玉米堆（2）"
                    );
                    list1.add(tmp1);
                }
                else{
                    list1.add(tmp);
                }
            }
//            if("2".equals(paymentManagement.getInvoiceFlag()) && paymentManagement.getRemarkss() != null){
//                paymentManagementList = list1;
//            }
            paymentManagementList = list1;
        }

        for(int i=0;i<paymentManagementList.size();i++){
            PaymentManagement temp = paymentManagementList.get(i);
            if("1".equals(temp.getInvoiceFlag())){
                str.add(xmlUtil.readXml(temp));
            }else if("2".equals(temp.getInvoiceFlag()) && temp.getRemarkss() != null){
                temp.setId(temp.getId()+i);
                str.add(xmlUtil.readsXml(temp));
            }else if("2".equals(temp.getInvoiceFlag())){
                str.add(xmlUtil.readXmls(temp));
            }
        }
        return str;
    }

    /**
     * 导出
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/export")
    public void export(@RequestBody PaymentManagement example, HttpServletResponse response) throws Exception {
        paymentManagementService.export(example,response);
    }

    /**
     * 手机审核文件下载
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/exportPhone")
    public String exportPhone(@RequestBody PaymentManagement example, HttpServletResponse response) throws Exception {
        return paymentManagementService.exportPhone(example,response);
    }

    /**
     * 手机审核数据
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/exportPhoneData")
    public PaymentView exportPhoneData(@RequestBody PaymentManagement paymentManagement) {
//        PaymentView paymentView =  paymentManagementService.exportPhoneData(example);
//        return paymentView;
        return paymentManagementService.exportPhoneData(paymentManagement);
    }
}

