package com.sifang.modules.supply.warehouse.service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.supply.warehouse.dao.CrmSupplyAllocationOrderDao;
import com.sifang.modules.supply.warehouse.dao.CrmSupplyAllocationOrderDetailDao;
import com.sifang.modules.supply.warehouse.dao.CrmSupplyPurchaseOrderDao;
import com.sifang.modules.supply.warehouse.dao.CrmSupplyPurchaseOrderDetailDao;
import com.sifang.modules.supply.warehouse.vo.AllocationOrderExcel;
import com.sifang.modules.supply.warehouse.vo.PurchaseOrderExcel;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.entity.Ascription;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.excel.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseExportService {

    @Autowired
    private CrmSupplyPurchaseOrderDao purchaseOrderDao;

    @Autowired
    private CrmSupplyPurchaseOrderDetailDao orderDetailDao;

    @Autowired
    private CrmSupplyAllocationOrderDao allocationOrderDao;

    @Autowired
    private CrmSupplyAllocationOrderDetailDao allocationOrderDetailDao;

    public void exportPurchaseOrder(Map<String, String> map, HttpServletResponse response) throws IOException {
        User user= UserUtils.getUser();
        String userType= CrmUtils.getCurrentUserType(user);
        if("2".equals(userType)){
            Ascription ascription=CrmUtils.getAscription();
            map.put("supplierId",ascription.getAscriptionId());
        }
        List<Record> list = purchaseOrderDao.getList(null, map);
        List<String> ids = new ArrayList<>();
        for (Record record : list) {
            ids.add(record.getStr("id"));
        }
        List<Record> details = orderDetailDao.getDetailsByOrderIds(ids);
        Map<String, List<Record>> detailsMap = new HashMap<>();
        for (Record detail : details) {
            List<Record> detailList = detailsMap.get(detail.getStr("purchase_id"));
            if (detailList != null) {
                detailList.add(detail);
            } else {
                List<Record> arr = new ArrayList<>();
                arr.add(detail);
                detailsMap.put(detail.getStr("purchase_id"), arr);
            }
        }
        List<PurchaseOrderExcel> orderExcels = new ArrayList<>();
        int sort = 1;
        for (Record rd : list) {
            List<Record> materials = detailsMap.get(rd.getStr("id"));
            for (Record material : materials) {
                PurchaseOrderExcel purchaseOrderExcel = new PurchaseOrderExcel();
                purchaseOrderExcel.setSort(String.valueOf(sort));
                purchaseOrderExcel.setNumber(rd.getStr("number"));
                purchaseOrderExcel.setSpaNumber(rd.getStr("spa_number"));
                purchaseOrderExcel.setStatus(transStatus(rd.getStr("status")));
                purchaseOrderExcel.setSupplierName(rd.getStr("supplierName"));
                purchaseOrderExcel.setCreator(rd.getStr("creator"));
                purchaseOrderExcel.setRemark(rd.getStr("remark"));
                purchaseOrderExcel.setWarehouseName(rd.getStr("warehouseName"));
                purchaseOrderExcel.setExamineRemark(rd.getStr("examine_remark"));
                purchaseOrderExcel.setCreate_time(rd.getDate("create_time"));
                purchaseOrderExcel.setConfirm_time(rd.getDate("confirm_time"));
                purchaseOrderExcel.setExamine_time(rd.getDate("examine_time"));
                purchaseOrderExcel.setFinish_time(rd.getDate("finish_time"));
                purchaseOrderExcel.setMaterial_code(material.getStr("material_code"));
                purchaseOrderExcel.setMaterial_type(material.getStr("material_type"));
                purchaseOrderExcel.setMaterial_group(material.getStr("material_group"));
                purchaseOrderExcel.setMaterial_unit(material.getStr("material_unit"));
                purchaseOrderExcel.setMaterial_remark(material.getStr("material_remark"));
                purchaseOrderExcel.setAmount(material.getBigDecimal("amount"));
                purchaseOrderExcel.setIn_amount(material.getBigDecimal("in_amount"));
                orderExcels.add(purchaseOrderExcel);
                sort++;
            }
        }
        String fileName = map.get("name") + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        new ExportExcel(map.get("name"), PurchaseOrderExcel.class)
                .setDataList(orderExcels)
                .write(response, fileName)
                .dispose();
    }

    private String transStatus(String status){   //1 待确认 2 待检验 3 部分检验 4 已完成 5已撤销
        switch (status) {
            case "1":
                return "待确认";
            case "2":
                return "待检验";
            case "3":
                return "部分检验";
            case "4":
                return "已完成";
            case "5":
                return "已撤销";
            default:
                return "";
        }
    }


    public void exportAllocationOrder(Map<String, String> map, HttpServletResponse response) throws IOException {
        User user = UserUtils.getUser();
        String userType = CrmUtils.getCurrentUserType(user);
        if(!"1".equals(userType)){
            Ascription ascription=CrmUtils.getAscription();
            map.put("ascriptionId",ascription.getAscriptionId());
        }
        List<Record> list = allocationOrderDao.getOrderList(null, map,user);
        List<String> ids = new ArrayList<>();
        for (Record record : list) {
            ids.add(record.getStr("id"));
        }
        List<Record> details = allocationOrderDetailDao.getDetailsByOrderIds(ids);
        Map<String, List<Record>> detailsMap = new HashMap<>();
        for (Record detail : details) {
            List<Record> detailList = detailsMap.get(detail.getStr("inventory_id"));
            if (detailList != null) {
                detailList.add(detail);
            } else {
                List<Record> arr = new ArrayList<>();
                arr.add(detail);
                detailsMap.put(detail.getStr("inventory_id"), arr);
            }
        }
        List<AllocationOrderExcel> orderExcels = new ArrayList<>();
        int sort = 1;
        for (Record rd : list) {
            List<Record> materials = detailsMap.get(rd.getStr("id"));
            for (Record material : materials) {
                AllocationOrderExcel allocationOrderExcel = new AllocationOrderExcel();
                allocationOrderExcel.setSort(String.valueOf(sort));
                allocationOrderExcel.setCode(rd.getStr("code"));
                allocationOrderExcel.setStatus(transStatus2(rd.getStr("status")));
                allocationOrderExcel.setOut_name(rd.getStr("out_name"));
                allocationOrderExcel.setWarehouse_name(rd.getStr("warehouse_name"));
                allocationOrderExcel.setIn_name(rd.getStr("in_name"));
                allocationOrderExcel.setIn_warehouse_name(rd.getStr("in_warehouse_name"));
                allocationOrderExcel.setRemarks(rd.getStr("remarks"));
                allocationOrderExcel.setCreate_time(rd.getDate("create_time"));
                allocationOrderExcel.setOut_time(rd.getDate("out_time"));
                allocationOrderExcel.setIn_time(rd.getDate("in_time"));
                allocationOrderExcel.setAudit_time(rd.getDate("audit_time"));
                allocationOrderExcel.setMaterial_code(material.getStr("material_code"));
                allocationOrderExcel.setMaterial_type(material.getStr("material_type"));
                allocationOrderExcel.setMaterial_group(material.getStr("material_group"));
                allocationOrderExcel.setMaterial_unit(material.getStr("material_unit"));
                allocationOrderExcel.setMaterial_remark(material.getStr("material_remarks"));
                allocationOrderExcel.setAmount(material.getBigDecimal("amount"));
                allocationOrderExcel.setInout_amount(material.getBigDecimal("inout_amount"));
                orderExcels.add(allocationOrderExcel);
                sort++;
            }
        }
        String fileName = map.get("name") + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        new ExportExcel(map.get("name"), AllocationOrderExcel.class)
                .setDataList(orderExcels)
                .write(response, fileName)
                .dispose();
    }

    private String transStatus2(String status){   //1.待调出方出库 2.待调入方入库 3.待审批 5.已完成
        switch (status) {
            case "1":
                return "待调出方出库";
            case "2":
                return "待调入方入库";
            case "3":
                return "审批中";
           /* case "4":
                return "入账未通过";*/
            case "5":
                return "已完成";
            default:
                return "";
        }
    }

}
