package com.jwipc.contract.controller.pc;

import cn.hutool.core.collection.CollUtil;
import com.jwipc.contract.constant.ModuleConst;
import com.jwipc.contract.dto.ContractPayDTO;
import com.jwipc.contract.dto.ContractPayTypeDTO;
import com.jwipc.contract.logic.ContractLogic;
import com.jwipc.contract.model.query.*;
import com.jwipc.exception.DaoException;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.clent.dto.SupplierBankDTO;
import com.jwipc.sys.client.PurchaseGroupClient;
import com.jwipc.sys.client.dto.PurchaseGroupDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能描述:
 *
 * @Author: zx
 * @Date: 2024/3/9 0009 17:25
 */

@RestController
@RequestMapping(ModuleConst.MODULE_ROOT + "/pay/rest")
@RequiredArgsConstructor
@Slf4j
public class ContractPayRestController {

    @Autowired
    ContractLogic contractLogic;
    @Autowired
    PurchaseGroupClient purchaseGroupClient;

    @AuditLogInter(value = "查询", module = "合同主数据")
    @RequestMapping("/index/page")
    public PageView listPage(ContractMangeQuery mangeQuery) {
        List<PurchaseGroupDTO> dtoList = purchaseGroupClient.listAll(LoginUtil.getUserId());
        //判断是否为空，不为空设置采购组list
        if(CollUtil.isNotEmpty(dtoList)){
            List<String> collect = dtoList.stream().map(PurchaseGroupDTO::getPurchaseGroupCode).collect(Collectors.toList());
            mangeQuery.setList(collect);
        }
        return contractLogic.contractPayListPage(mangeQuery);
    }


    @RequestMapping("/choseContractHistoryList")
    public PageView choseContractHistoryList(ChoseSupplierListQuery choseSupplierListQuery) {
        return contractLogic.choseContractHistoryList(choseSupplierListQuery);
    }


    @RequestMapping("/supplierBankDate")
    public List<SupplierBankDTO> supplierBankDate(String contractSupplierCode) {
        return contractLogic.supplierBankDate(contractSupplierCode);
    }

    @RequestMapping("/totalPayAmount")
    public String  totalPayAmount(String contractCode) {
        return contractLogic.getTotalPayAmount(contractCode);
    }

    @RequestMapping("/selectMroContractDetailListPage")
    public PageView selectMroContractDetailListPage(ChoseSupplierListQuery choseSupplierListQuery) {
        return contractLogic.selectMroContractDetailListPage(choseSupplierListQuery);
    }

    @RequestMapping("/detailLists")
    public PageView detailLists(ContractPayDetailsQuery payDetailsQuery) {
        return contractLogic.detailLists(payDetailsQuery);
    }

    @RequestMapping("/detailOrderLists")
    public PageView detailOrderLists(ContractPayDetailsQuery payDetailsQuery) {
        return contractLogic.detailOrderLists(payDetailsQuery);
    }

    @RequestMapping("/detailTypeLists")
    public PageView detailTypeLists(ContractPayDetailsQuery payDetailsQuery) {
        return contractLogic.detailTypeLists(payDetailsQuery);
    }

    @RequestMapping("/detailTypeListByContractId")
    public  ResponseEntity detailTypeListByContractId(Long contractId) {
        return ResponseEntity.ok(contractLogic.detailTypeListByContractId(contractId));
    }


    @RequestMapping("/amountSum")
    public Map<Long, BigDecimal> amountSum(String payStageIds, String contractIds, String payId) {
        return contractLogic.amountSum(payStageIds,contractIds,payId);
    }


    @RequestMapping("/form/formPayData")
    @Transactional
    public ResponseEntity formPayData(ContractPayDTO bean, String dlist,String orderlist,String typelist ) {
        //检查编码是否存在
        Long count = contractLogic.selectOnlyPayCode(bean.getPayCode(), bean.getId());
        if (count > 0) {
            throw new DaoException("编码已存在，请修改!");
        }
        if (!StringUtils.isEmpty(bean.getId())) {
            bean.setUpdateTime(LocalDateTime.now());
            bean.setUpdateUser(LoginUtil.getUserId());
        }
        contractLogic.saveContractPayDeatils(bean,dlist,orderlist,typelist);
        return ResponseEntity.ok(true);
    }


    @RequestMapping("/remove")
    @Transactional
    public ResponseEntity remove(String id) {
        contractLogic.removeContractPay(id);
        return ResponseEntity.ok(true);
    }


    @RequestMapping("/getPayBillByKm")
    @Transactional
    public ResponseEntity getPayBillByKm(String payId,String oaApplyId) {
        return ResponseEntity.ok(contractLogic.getPayBillByKm(payId,oaApplyId));
    }


    @RequestMapping("/getOaProcessList")
    @Transactional
    public PageView getOaProcessList(ProcessListQuery processListQuery) {
        return contractLogic.getOaProcessList(processListQuery);
    }


}
