package com.jwipc.contract.controller.pc;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.contract.constant.ContractConst;
import com.jwipc.contract.constant.ModuleConst;
import com.jwipc.contract.dto.ContractDTO;
import com.jwipc.contract.dto.ContractDetailDTO;
import com.jwipc.contract.dto.ContractMroDTO;
import com.jwipc.contract.logic.ContractLogic;
import com.jwipc.inquiry.client.InquiryClient;
import com.jwipc.inquiry.dto.InquiryDetailByIdDTO;
import com.jwipc.inquiry.dto.InquiryDetailByIdDetailClientDTO;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.supplier.client.TenderClient;
import com.jwipc.sys.client.*;
import com.jwipc.sys.client.dto.CompanyDTO;
import com.jwipc.sys.client.dto.FactoryDTO;
import com.jwipc.sys.client.dto.TaxDTO;
import com.jwipc.tender.client.dto.TenderDetailByIdDetailDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 合同管理控制器
 */
@Controller
@RequestMapping(ModuleConst.MODULE_ROOT + "/manage/page")
@RequiredArgsConstructor
@Slf4j
public class ContractPageController {

    @Autowired
    ContractLogic contractLogic;

    @Autowired
    SequenceClient sequenceClient;

    @Autowired
    InquiryClient inquiryClient;

    @Autowired
    CompanyClient companyClient;

    @Autowired
    FactoryClient factoryClient;

    @Autowired
    SupplierClient supplierClient;

    @Autowired
    TaxClient taxClient;

    @Autowired
    UserPurchaseGroupClient userPurchaseGroupClient;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    TenderClient tenderClient;

    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("supplierFlag", false);
        return ModuleConst.MODULE_ROOT + "/manage/index";
    }

    @RequestMapping("/report/index")
    public String reportIndex(Model model){
        model.addAttribute("supplierFlag", false);
        return ModuleConst.MODULE_ROOT + "/manage/report/index";
    }


    @RequestMapping("/supplier/index")
    public String supplierIndex(Model model){
        model.addAttribute("supplierFlag", true);
        return ModuleConst.MODULE_ROOT + "/manage/index";
    }



    @AuditLogInter(value = "新增或编辑", module = "合同主数据")
    @RequestMapping("/form")
    public String form( Model model, String id,Integer check) {
        ContractDTO bean = contractLogic.formDeatil(id) ;
        if(StringUtils.isEmpty(id)){
//            String code = sequenceClient.createSequenceNo("contract_code");
            String code = sequenceClient.getCodeSequenceNo("contract_code", 5);
            model.addAttribute("code", code);
        }
        CurrentUserDTO currentUserDTO = LoginUtil.getCurrentUser();
        model.addAttribute("userName", currentUserDTO.getName());
        model.addAttribute("userAccount", LoginUtil.getAccount());
        model.addAttribute("userDeptName", currentUserDTO.getDeptName());
        model.addAttribute("bean", bean);
        model.addAttribute("check", check);
        return ModuleConst.MODULE_ROOT + "/manage/form";
    }

    @AuditLogInter(value = "查看", module = "合同主数据")
    @RequestMapping("/look")
    public String LOOK( Model model, String id,Integer check) {
        ContractDTO bean = contractLogic.formDeatil(id) ;
        if(StringUtils.isEmpty(id)){
//            String code = sequenceClient.createSequenceNo("contract_code");
            String code = sequenceClient.getCodeSequenceNo("contract_code", 5);
            model.addAttribute("code", code);
        }
        CurrentUserDTO currentUserDTO = LoginUtil.getCurrentUser();
        model.addAttribute("userName", currentUserDTO.getLoginName());
        model.addAttribute("userAccount", LoginUtil.getAccount());
        model.addAttribute("userDeptName", currentUserDTO.getDeptName());
        model.addAttribute("bean", bean);
        model.addAttribute("check", check);
        return ModuleConst.MODULE_ROOT + "/manage/look";
    }


    @RequestMapping("/formDeatilById")
    public String formDeatilById( Model model,String supplierId,String supplierCode,String supplierName,String uuidKey, Integer check) {
        InquiryDetailByIdDTO detail = null;
        String rowKeyValue = (String)redisTemplate.opsForValue().get(uuidKey);
        List<InquiryDetailByIdDTO> list = JSON.parseArray(rowKeyValue,InquiryDetailByIdDTO.class);
        Map<String,List<InquiryDetailByIdDTO>> maps = list.stream().collect(Collectors.groupingBy(InquiryDetailByIdDTO::getSupplierId));
        List<InquiryDetailByIdDTO> details = maps.get(supplierId);
        detail = details.get(0);
        if(!StringUtils.isEmpty(supplierCode)){
            detail.setSupplierCode(supplierCode);
        }
        if(!StringUtils.isEmpty(supplierName)){
            detail.setSupplierName(supplierName);
        }
        List<ContractDetailDTO> items = new ArrayList<>();
        if(detail.getDataSources().equals(ContractConst.DATASOURCES_XJ)){
            List<InquiryDetailByIdDetailClientDTO> detailByIdDetailPOS = inquiryClient.formDeatilById(detail.getId(),detail.getSupplierId());
            items = JSON.parseArray(JSON.toJSONString(detailByIdDetailPOS),ContractDetailDTO.class);
            detail.setDataSources(2);
        }else{
            List<TenderDetailByIdDetailDTO> detailByIdDetailPOS = tenderClient.formDeatilById(detail.getId(),detail.getSupplierId());
            items = JSON.parseArray(JSON.toJSONString(detailByIdDetailPOS),ContractDetailDTO.class);
            detail.setDataSources(1);
        }
        CompanyDTO companyDTO = companyClient.getByCode(detail.getContractCompanyCode());
        if(companyDTO!=null){
            detail.setContractCompanyName(companyDTO.getCompanyName());
        }
        model.addAttribute("details", null);
        final BigDecimal[] contractAmount = {BigDecimal.ZERO};
        if(CollectionUtils.isNotEmpty(items)){
            items.stream().forEach(a->{
                FactoryDTO factoryDTO = factoryClient.getByCode(a.getFactoryCode());
                TaxDTO taxDTO = taxClient.getByCode(a.getTaxRateCode());
                if(!StringUtils.isEmpty(taxDTO)){
                    a.setTaxRateName(taxDTO.getTaxName());
                }
                if(!StringUtils.isEmpty(factoryDTO)){
                    a.setFactoryName(factoryDTO.getName());
                }
                contractAmount[0] = contractAmount[0].add(new BigDecimal(a.getTotle())).setScale(4,BigDecimal.ROUND_HALF_UP);
                a.setMaterialName(!StringUtils.isEmpty(a.getMaterialName())?a.getMaterialName().replace("'",""):"");
                a.setMaterialModel(!StringUtils.isEmpty(a.getMaterialModel())?a.getMaterialModel().replace("'",""):"");
                a.setMaterialSpec(!StringUtils.isEmpty(a.getMaterialSpec())?a.getMaterialSpec().replace("'",""):"");
            });
            detail.setCurrency(items.get(0).getCurrencyCode());
            detail.setContractAmount(contractAmount[0]);
            model.addAttribute("details", JSON.toJSONString(detail.getItems()));
        }
//        String code = sequenceClient.createSequenceNo("contract_code");
        String code = sequenceClient.getCodeSequenceNo("contract_code", 5);
        detail.setContractCode(code);
        CurrentUserDTO currentUserDTO = LoginUtil.getCurrentUser();
        model.addAttribute("userName", currentUserDTO.getLoginName());
        model.addAttribute("userAccount", LoginUtil.getAccount());
        model.addAttribute("userDeptName", currentUserDTO.getDeptName());
        model.addAttribute("bean", detail);
        model.addAttribute("items", JSON.toJSONString(items));
        model.addAttribute("check", check);
        return ModuleConst.MODULE_ROOT + "/manage/formDeatilById";
    }

    @RequestMapping("/formBatchDetailByIds")
    public String formBatchDetailByIds( Model model,String ids,String supplierId,String supplierCode,
                                        String supplierName,String uuidKey, Integer check, String tmIdList) {
        InquiryDetailByIdDTO detail = new InquiryDetailByIdDTO();
        if(!StringUtils.isEmpty(supplierCode)){
            detail.setSupplierCode(supplierCode);
        }
        if(!StringUtils.isEmpty(supplierName)){
            detail.setSupplierName(supplierName);
        }
        List<ContractDetailDTO> items = new ArrayList<>();
        List<String> nos = new ArrayList<>();
        List<String> inquiryIdList =  StrUtil.split(ids, ",");
        List<InquiryDetailByIdDetailClientDTO> detailByIdDetailPOS = inquiryClient.formDetailByMaterialSupplierId(inquiryIdList,supplierId);
        List<String> tmIdsList =  StrUtil.split(tmIdList, ",");
        if(!CollectionUtils.isEmpty(tmIdsList)){
            detailByIdDetailPOS=detailByIdDetailPOS.stream()
                    .filter(detail1 -> tmIdsList.stream().anyMatch(keyword -> detail1.getTmId().toString().contains(keyword)))
                    .collect(Collectors.toList());
        }
        detailByIdDetailPOS.forEach(inquiryDetailByIdDetailClientDTO -> {
            nos.add(inquiryDetailByIdDetailClientDTO.getDataSourcesNo());
        });
        if (CollectionUtils.isNotEmpty(nos)){
            detail.setDataSourcesNo(org.apache.commons.lang3.StringUtils.join(nos, ","));
        }
        items = JSON.parseArray(JSON.toJSONString(detailByIdDetailPOS),ContractDetailDTO.class);
        detail.setDataSources(3);
        CompanyDTO companyDTO = companyClient.getByCode("1000");
        if(companyDTO!=null){
            detail.setContractCompanyName(companyDTO.getCompanyName());
        }
        model.addAttribute("details", null);
        final BigDecimal[] contractAmount = {BigDecimal.ZERO};
        if(CollectionUtils.isNotEmpty(items)){
            items.stream().forEach(a->{
                FactoryDTO factoryDTO = factoryClient.getByCode(a.getFactoryCode());
                TaxDTO taxDTO = taxClient.getByCode(a.getTaxRateCode());
                if(!StringUtils.isEmpty(taxDTO)){
                    a.setTaxRateName(taxDTO.getTaxName());
                }
                if(!StringUtils.isEmpty(factoryDTO)){
                    a.setFactoryName(factoryDTO.getName());
                }
                contractAmount[0] = contractAmount[0].add(new BigDecimal(a.getTotle())).setScale(4,BigDecimal.ROUND_HALF_UP);
                a.setMaterialName(!StringUtils.isEmpty(a.getMaterialName())?a.getMaterialName().replace("'",""):"");
                a.setMaterialModel(!StringUtils.isEmpty(a.getMaterialModel())?a.getMaterialModel().replace("'",""):"");
                a.setMaterialSpec(!StringUtils.isEmpty(a.getMaterialSpec())?a.getMaterialSpec().replace("'",""):"");
            });
            detail.setCurrency(items.get(0).getCurrencyCode());
            detail.setContractAmount(contractAmount[0]);
            model.addAttribute("details", JSON.toJSONString(detail.getItems()));
        }
//        String code = sequenceClient.createSequenceNo("contract_code");
        String code = sequenceClient.getCodeSequenceNo("contract_code", 5);

        detail.setContractCode(code);
        CurrentUserDTO currentUserDTO = LoginUtil.getCurrentUser();
        model.addAttribute("userName", currentUserDTO.getLoginName());
        model.addAttribute("userAccount", LoginUtil.getAccount());
        model.addAttribute("userDeptName", currentUserDTO.getDeptName());
        model.addAttribute("bean", detail);
        model.addAttribute("items", JSON.toJSONString(items));
        model.addAttribute("check", check);
        return ModuleConst.MODULE_ROOT + "/manage/formDeatilById";
    }

    @RequestMapping("/choseSupplier")
    public String choseSupplier( Model model, Integer type ) {
        model.addAttribute("type", type);
        return ModuleConst.MODULE_ROOT + "/manage/choseSupplier";
    }


    @RequestMapping("/choseContract")
    public String choseContract( Model model) {
        return ModuleConst.MODULE_ROOT + "/manage/choseContract";
    }


    @RequestMapping("/formPay")
    public String fromPayDetail(Model model, Integer conId, Long rowId,String paymentStage,
                                String paymentTime , BigDecimal paymentRatio,String remark) {
        model.addAttribute("rowId", rowId);
        model.addAttribute("paymentStage", paymentStage);
        model.addAttribute("paymentTime", paymentTime);
        model.addAttribute("paymentRatio", paymentRatio!=null?paymentRatio:BigDecimal.ZERO);
        model.addAttribute("conId", conId);
        model.addAttribute("remark", remark);
        return ModuleConst.MODULE_ROOT + "/manage/formPay";
    }


    @RequestMapping("/formDetail")
    public String formDeatil( Model model, String detailId,String isSap,Integer isMro,String subjectCode,String costCenterCode,String costCenterName,String materialName,String materialGroupCode,String materialGroupName,String materialCategory,String materialSpec,Integer warrantyPeriod,String materialModel,String materialBrand,String basisUnit,String price,String num,String taxRateName,String cardNumber,String taxRateCode,String bz,String currencyCode,String purchaseCode,String factoryCode,String factoryName,String itemCode,String customerCode,String customerName,String driverAsset,String teamName,String requireName) {
        ContractDetailDTO detail=null ;
        if(StringUtils.isEmpty(detailId) || detailId.equals("undefined")){
            if(!StringUtils.isEmpty(subjectCode)) {
                detail =new ContractDetailDTO();
                detail.setSubjectCode(subjectCode.equals("undefined")?null:subjectCode);
                //detail.setMaterialCode(materialCode);
                detail.setMaterialName(materialName);
                detail.setMaterialGroupCode(materialGroupCode);
                detail.setMaterialGroupName(materialGroupName);
                detail.setMaterialCategory(materialCategory.equals("undefined")?null:materialCategory);
                detail.setMaterialSpec(materialSpec.equals("undefined")?null:materialSpec);
                detail.setWarrantyPeriod(warrantyPeriod==null?null:warrantyPeriod);
                detail.setMaterialModel(materialModel.equals("undefined")?null:materialSpec);
                detail.setMaterialBrand(materialBrand.equals("undefined")?null:materialBrand);
                detail.setBasisUnit(basisUnit);
                detail.setPrice(StringUtils.isEmpty(price)||price.equals("undefined") ? BigDecimal.ZERO : new BigDecimal(price));
                detail.setNum(StringUtils.isEmpty(num)||num.equals("undefined")  ? BigDecimal.ZERO:new BigDecimal(num));
                detail.setTaxRateName(taxRateName);
                detail.setTaxRateCode(taxRateCode);
                detail.setCardNumber(cardNumber.equals("undefined")?null:cardNumber);
                detail.setPurchaseCode(purchaseCode.equals("undefined")?null:purchaseCode);
                detail.setItemCode(itemCode.equals("undefined")?null:itemCode);
                detail.setFactoryCode(factoryCode==null?"":factoryCode);
                detail.setFactoryName(factoryName==null?"":factoryName);
                detail.setBz(bz.equals("undefined")?null:bz);
                detail.setCurrencyCode(currencyCode);
                detail.setCustomerCode(customerCode.equals("undefined")?null:customerCode);
                detail.setCustomerName(customerName.equals("undefined")?null:customerName);
                detail.setDriverAsset(driverAsset.equals("undefined")?null:driverAsset);
                detail.setTeamName(teamName.equals("undefined")?null:teamName);
                detail.setRequireName(requireName.equals("undefined")?null:requireName);
                detail.setCostCenterCode(costCenterName.equals("undefined")?null:costCenterCode);
                detail.setCostCenterName(costCenterName.equals("undefined")?null:costCenterName);
            }

        }else{
            detail = contractLogic.formDetail(detailId);
            detail.setSubjectCode(subjectCode.equals("undefined")?null:subjectCode);
            //detail.setMaterialCode(materialCode);
            detail.setMaterialName(materialName);
            detail.setMaterialGroupCode(materialGroupCode);
            detail.setMaterialGroupName(materialGroupName);
            detail.setMaterialCategory(materialCategory.equals("undefined")?null:materialCategory);
            detail.setMaterialSpec(materialSpec.equals("undefined")?null:materialSpec);
            detail.setWarrantyPeriod(warrantyPeriod==null?null:warrantyPeriod);
            detail.setMaterialModel(materialModel.equals("undefined")?null:materialSpec);
            detail.setMaterialBrand(materialBrand.equals("undefined")?null:materialBrand);
            detail.setBasisUnit(basisUnit);
            detail.setPrice(StringUtils.isEmpty(price)||price.equals("undefined") ? BigDecimal.ZERO : new BigDecimal(price));
            detail.setNum(StringUtils.isEmpty(num)||num.equals("undefined")  ? BigDecimal.ZERO:new BigDecimal(num));
            detail.setTaxRateName(taxRateName);
            detail.setTaxRateCode(taxRateCode);
            detail.setCardNumber(cardNumber.equals("undefined")?null:cardNumber);
            detail.setPurchaseCode(purchaseCode.equals("undefined")?null:purchaseCode);
            detail.setItemCode(itemCode.equals("undefined")?null:itemCode);
            detail.setFactoryCode(factoryCode);
            detail.setFactoryName(factoryName);
            detail.setBz(bz.equals("undefined")?null:bz);
            detail.setCurrencyCode(currencyCode);
            detail.setCustomerCode(customerCode.equals("undefined")?null:customerCode);
            detail.setCustomerName(customerName.equals("undefined")?null:customerName);
            detail.setDriverAsset(driverAsset.equals("undefined")?null:driverAsset);
            detail.setTeamName(teamName);
            detail.setRequireName(requireName);
            detail.setCostCenterCode(costCenterCode.equals("undefined")?null:costCenterCode);
            detail.setCostCenterName(costCenterName.equals("undefined")?null:costCenterName);
        }
        model.addAttribute("detail", detail);
        model.addAttribute("isSap", isSap);
        model.addAttribute("isMro", isMro==null?1:isMro);
        return ModuleConst.MODULE_ROOT + "/manage/formDetail";
    }


    @RequestMapping("/formDetailMro")
    public String formDetailMro(Model model, String detailId, Long conId, Long rowId, String name, String num, String signTime, String ourPeople, String otherPeople, BigDecimal amount) {
       if(StringUtils.isEmpty(detailId)){
           ContractMroDTO dto = new ContractMroDTO();
           dto.setContractId(conId);
           dto.setName(name);
           dto.setNum(num);
           dto.setAmount(amount);
           DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
           if(!StringUtils.isEmpty(signTime)){
               dto.setSignTime( LocalDate.parse(signTime, formatter));
           }
           dto.setOtherPeople(otherPeople);
           dto.setOurPeople(ourPeople);
           model.addAttribute("detail", dto);
       }else{
           ContractMroDTO mroDTO = contractLogic.formDetailMro(detailId);
           model.addAttribute("detail",mroDTO);
       }
        return ModuleConst.MODULE_ROOT + "/manage/formDetailMro";
    }

    @GetMapping("/updateTimePage")
    public String updateTimePage(String ids, Model model){
        model.addAttribute("ids",ids);
        return ModuleConst.MODULE_ROOT + "/manage/report/update_time_page";
    }
}
