package com.dk.mdm.service.report;

import com.alibaba.fastjson.JSONObject;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.infrastructure.enums.ErrorCodeEnum;
import com.dk.common.mapper.BaseMapper;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseCodeEnum;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.common.service.BaseService;
import com.dk.mdm.infrastructure.util.AuthUtils;
import com.dk.mdm.mapper.common.CommonMapper;
import com.dk.mdm.mapper.ivt.InboundItemMapper;
import com.dk.mdm.mapper.ivt.OutboundItemMapper;
import com.dk.mdm.mapper.mst.StaffMapper;
import com.dk.mdm.mapper.report.ReportMapper;
import lombok.extern.slf4j.Slf4j;
import org.postgresql.util.PGobject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc   : 报表数据源
 * @author : 常皓宁
 * @date   : 2024/4/11 8:49
 */
@Service
@Slf4j
public class ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private InboundItemMapper inboundItemMapper;

    @Autowired
    private OutboundItemMapper outboundItemMapper;
    /**
     * @desc   : 查询库存
     * @author : 常皓宁
     * @date   : 2024/4/11 8:52
     */
    public ResponseResultVO<List<Map<String, Object>>> getIvtDetailReport(Map<String, Object> param) {
        // 获取系统基础数据
        List<Map<String, Object>> list = reportMapper.getIvtDetailReport(param);
        return ResponseResultUtil.success(list);
    }
    /**
     * @desc   : 采购明细报表（明细）
     * @author : 宋扬
     * @date   : 2024/4/10 16:39
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getPurDetailReport(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 获取系统基础数据
        List<Map<String, Object>> list=new ArrayList<>();
        PageList data = new PageList<>();
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(param.get("flgViewItem")==null){
            // 查询主表
            dataList= reportMapper.getPurReport(param);
        }else {
            // 查询明细
            dataList= reportMapper.getPurDetailReport(param);
        }
        data.setList(dataList);
        data.setTotal(reportMapper.getSalesTrackingReportCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }

    /**
     * @desc   : 销售明细报表
     * @author : 宋扬
     * @date   : 2024/4/11 13:22
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getOrderDetailReport(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 获取系统基础数据
        List<Map<String, Object>> list=new ArrayList<>();
        PageList data = new PageList<>();
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(param.get("flgViewItem")==null){
            // 查询主表
            dataList= reportMapper.getOrderReport(param);
        }else {
            // 查询明细
            dataList= reportMapper.getOrderDetailReport(param);
        }
        data.setList(dataList);
        data.setTotal(reportMapper.getSalesTrackingReportCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }

    /**
     * @desc   : 出库明细报表
     * @author : 宋扬
     * @date   : 2024/4/11 16:54
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getOutboundDetailReport(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 获取系统基础数据
        PageList data = new PageList<>();
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(param.get("flgViewItem")==null){
            // 查询主表
            dataList= reportMapper.getOutboundReport(param);
        }else {
            // 查询明细
            dataList= reportMapper.getOutboundDetailReport(param);
        }
        data.setList(dataList);
        data.setTotal(reportMapper.getSalesTrackingReportCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }

    /**
     * @desc   : 入库明细报表
     * @author : 宋扬
     * @date   : 2024/4/11 16:54
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getInboundDetailReport(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 获取系统基础数据
        PageList data = new PageList<>();
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(param.get("flgViewItem")==null){
            // 查询主表
            dataList= reportMapper.getInboundlReport(param);
        }else {
            // 查询明细
            dataList= reportMapper.getInboundDetailReport(param);
        }
        data.setList(dataList);
        data.setTotal(reportMapper.getSalesTrackingReportCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }

    /**
     * @desc   : 客户收款明细表
     * @author : 宋扬
     * @date   : 2024/4/16 11:29
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getRecPayDetailReport(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 获取系统基础数据
        PageList data = new PageList<>();
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        // 查询主表
        List<Map<String, Object>> dataList = reportMapper.getRecPayDetailReport(param);
        data.setList(dataList);
        data.setTotal(reportMapper.getRecPayDetailReportCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }


    /**
     * @desc   : 销售跟踪表 列表查询
     * @author : 于继渤
     * @date   : 2024/4/16 11:27
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getSalesTrackingReport(Map<String, Object> param) {

        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 定义返回值
        PageList data = new PageList<>();
        // 获取SKU商品
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = reportMapper.getSalesTrackingReport(param);
        data.setList(dataList);
        data.setTotal(reportMapper.getSalesTrackingReportCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }


    /**
     * @desc   : 销售明细
     * @author : 于继渤
     * @date   : 2024/4/16 11:27
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getSalesItemReport(Map<String, Object> param) {

        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 定义返回值
        PageList data = new PageList<>();
        // 获取SKU商品
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = reportMapper.getSalesItemReport(param);
        data.setList(dataList);
        data.setTotal(reportMapper.getSalesItemReportCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }


    /**
     * @desc   : 采购跟踪表 列表查询
     * @author : 王英杰
     * @date   : 2024/4/16 11:27
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getPurTrackingReport(Map<String, Object> param) {

        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 定义返回值
        PageList data = new PageList<>();
        // 获取SKU商品
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = reportMapper.getPurTrackingReport(param);
        data.setList(dataList);
        data.setTotal(reportMapper.getPurTrackingReportCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }


    /**
     * @desc   : 采购跟踪表 明细查询
     * @author : 王英杰
     * @date   : 2024/4/16 11:27
     */
    public ResponseResultVO getPurTrackingReportDetail(String purId) {

        return ResponseResultUtil.success(reportMapper.getPurTrackingReportDetail(purId) );
    }

    /**
     * @desc   : 销售跟踪表 明细查询
     * @author : 于继渤
     * @date   : 2024/4/16 11:27
     */
    public ResponseResultVO getSalesTrackingReportDetail(String id) {
        return ResponseResultUtil.success(reportMapper.getSalesTrackingReportDetail(id) );
    }


    /**
     * @desc   : 日结对账表
     * @date   : 2024/4/24 15:11
     * @author : 寇珊珊
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getDailyReconciliationReport(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 定义返回值
        PageList data = new PageList<>();
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = reportMapper.getDailyReconciliationReport(param);
        data.setList(dataList);
        data.setTotal(reportMapper.getDailyReconciliationReportCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }

    /**
     * @desc   :  日结对账表-查销售订单
     * @date   : 2024/4/24 15:11
     * @author : 寇珊珊
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getDailyReconciliationReportByOrder(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 定义返回值
        PageList data = new PageList<>();
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = reportMapper.getDailyReconciliationReportByOrder(param);
        data.setList(dataList);
        data.setTotal(reportMapper.getDailyReconciliationReportByOrderCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }


    /**
     * @desc   :  日结对账表-查销售出库
     * @date   : 2024/4/24 15:11
     * @author : 寇珊珊
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getDailyReconciliationReportByOut(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 定义返回值
        PageList data = new PageList<>();
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = reportMapper.getDailyReconciliationReportByOut(param);
        data.setList(dataList);
        data.setTotal(reportMapper.getDailyReconciliationReportByOutCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }


    /**
     * @desc   :  日结对账表-查收款单
     * @date   : 2024/4/24 15:11
     * @author : 寇珊珊
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getDailyReconciliationReportByPay(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 定义返回值
        PageList data = new PageList<>();
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        List<Map<String, Object>> dataList = reportMapper.getDailyReconciliationReportByPay(param);
        data.setList(dataList);
        data.setTotal(reportMapper.getDailyReconciliationReportByPayCount(param));
        data.setPageSize((Integer) param.get("pageSize"));
        data.setCurrentPage((Integer) param.get("currentPage"));
        return ResponseResultUtil.success(data);
    }

    /**
     * @desc   : 收发存汇总报表
     * @author : 宋扬
     * @date   : 2024/4/20 13:30
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getIvtSumReport(Map<String, Object> param) {
        if (param.get("querys") != null) {
            JSONObject querys = JSONObject.parseObject(param.get("querys").toString());
            param.put("querys",querys);
        }

        List<Map<String, Object>> list = reportMapper.getIvtSumReport(param);
        JSONObject jObject = JSONObject.parseObject(list.get(0).get("f_query_ivt_rds").toString());
        List dataList = jObject.get("data") != null ? JSONObject.parseArray(jObject.get("data").toString())
                .toJavaList(Map.class) : new ArrayList();

        // 定义返回值
        PageList data = new PageList<>();
        data.setPageSize((int) param.get("pageSize")).setCurrentPage((int) param.get("currentPage"))
                .setTotal(Long.parseLong(jObject.get("pageTotal").toString()))
                .setList(dataList);

        if ("查询成功".equals(jObject.get("message").toString())) {
            return ResponseResultUtil.success(data);
        } else {
            return ResponseResultUtil.error(666, jObject.get("message").toString());
        }
    }

    /**
     * @desc   : 收发存汇总报表-明细
     * @author : 宋扬
     * @date   : 2024/4/23 14:10
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getIvtItemReport(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }

        List<Map<String, Object>> list = reportMapper.getIvtItemReport(param);
        JSONObject jObject = JSONObject.parseObject(list.get(0).get("f_query_ivt_rds_item").toString());
        List dataList = jObject.get("data") != null ? JSONObject.parseArray(jObject.get("data").toString())
                .toJavaList(Map.class) : new ArrayList();

        // 定义返回值
        PageList data = new PageList<>();
        data.setPageSize((int) param.get("pageSize")).setCurrentPage((int) param.get("currentPage"))
                .setTotal(Long.parseLong(jObject.get("pageTotal").toString()))
                .setList(dataList);

        if ("查询成功".equals(jObject.get("message").toString())) {
            return ResponseResultUtil.success(data);
        } else {
            return ResponseResultUtil.error(666, jObject.get("message").toString());
        }
    }

    /**
     * @desc   : 供应商往来余额表
     * @author : 常皓宁
     * @date   : 2024/4/25 13:27
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getSupAccount(Map<String, Object> param) {
        param.put("start", (Integer.parseInt(param.get("currentPage").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
        param.put("end", Integer.parseInt(param.get("pageSize").toString()));
        // 获取系统基础数据
        List<Map<String, Object>> list = reportMapper.getSupAccount(param);
        PageList data = new PageList<>();
        data.setList(list);
        return ResponseResultUtil.success(data);
    }

    /**
     * @desc   : 供应商往来余额表明细
     * @author : 常皓宁
     * @date   : 2024/4/25 13:27
     */
    public ResponseResultVO<List<Map<String, Object>>> getSupAccountItem(Map<String, Object> param) {
        // 获取系统基础数据
        List<Map<String, Object>> list = reportMapper.getSupAccountItem(param);
        return ResponseResultUtil.success(list);
    }

    /**
     * @desc   : 资金日记账
     * @author : 常皓宁
     * @date   : 2024/4/26 16:43
     */
    public ResponseResultVO<PageList<Map<String, Object>>> getFundReport(Map<String, Object> param) {
        // 校验分页参数
        if (param.get("pageSize") == null || param.get("currentPage") == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.OPERATE_FAIL, "请检查分页参数!");
        }
        // 获取资金日记账
        List<Map<String, Object>> list = reportMapper.getFundReport(param);
        JSONObject jObject = JSONObject.parseObject(list.get(0).get("f_get_fund_report").toString());
        List dataList = jObject.get("data") != null ? JSONObject.parseArray(jObject.get("data").toString())
                .toJavaList(Map.class) : new ArrayList();

        // 定义返回值
        PageList data = new PageList<>();
        data.setPageSize((int) param.get("pageSize")).setCurrentPage((int) param.get("currentPage"))
                .setTotal(Long.parseLong(jObject.get("pageTotal").toString()))
                .setList(dataList);

        if ("查询成功".equals(jObject.get("message").toString())) {
            return ResponseResultUtil.success(data);
        } else {
            return ResponseResultUtil.error(666, jObject.get("message").toString());
        }
    }


    /**
     * @desc   : 销售简报
     * @date   : 2024/5/6 8:43
     * @author : 寇珊珊
     */
    public ResponseResultVO<Map<String,Object>> getReportBatch(Map<String,Map<String,Object>> param){
        Map<String,Object> result = new HashMap<>();
        for(String key : param.keySet()){
            JSONObject valueObj =  JSONObject.parseObject(reportMapper.getReportBatch(param.get(key)).get("report").getValue());
            result.put(key,valueObj);
        }
        return ResponseResultUtil.success(result);
    }


}
