package com.jwipc.contract.controller.pc;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.contract.constant.ModuleConst;
import com.jwipc.contract.dto.ContractPayStageDTO;
import com.jwipc.contract.logic.ContractPayStageLogic;
import com.jwipc.contract.model.query.ContractPayStageQuery;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.PurchaseGroupClient;
import com.jwipc.sys.client.dto.PurchaseGroupDTO;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shenyang.ou
 * @date 2024/6/13 11:08
 * @desc
 */
@RestController
@RequestMapping(ModuleConst.MODULE_ROOT + "/pay/stage/rest")
@RequiredArgsConstructor
@Slf4j
public class ContractPayStageRestController{
    private final ContractPayStageLogic contractPayStageLogic;
    @Autowired
    PurchaseGroupClient purchaseGroupClient;

    @AuditLogInter(value = "查询", module = "合同应付款")
    @RequestMapping("/list")
    public PageView list(ContractPayStageQuery query) {
        List<PurchaseGroupDTO> dtoList = purchaseGroupClient.listAll(LoginUtil.getUserId());
        //判断是否为空，不为空设置采购组list
        if(CollUtil.isNotEmpty(dtoList)){
            List<String> collect = dtoList.stream().map(PurchaseGroupDTO::getPurchaseGroupCode).collect(Collectors.toList());
            query.setList(collect);
        }
        return contractPayStageLogic.findByPage(query);
    }
    /**
     * Dimension
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        ContractPayStageQuery contractPayStageQuery = JSON.parseObject(queryStr, ContractPayStageQuery.class);
        List<PurchaseGroupDTO> dtoList = purchaseGroupClient.listAll(LoginUtil.getUserId());
        //判断是否为空，不为空设置采购组list
        if(CollUtil.isNotEmpty(dtoList)){
            List<String> collect = dtoList.stream().map(PurchaseGroupDTO::getPurchaseGroupCode).collect(Collectors.toList());
            contractPayStageQuery.setList(collect);
        }
        final List<ContractPayStageDTO> exportData = contractPayStageLogic.listAll(contractPayStageQuery);
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"合同应付款记录-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }

}
