package cn.iocoder.yudao.module.stockTransfer.controller.admin;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.company.entity.Company;
import cn.iocoder.yudao.module.company.service.CompanyService;
import cn.iocoder.yudao.module.project.entity.RepairWares;
import cn.iocoder.yudao.module.stockTransfer.entity.DlRepairSt;
import cn.iocoder.yudao.module.stockTransfer.entity.DlRepairSti;
import cn.iocoder.yudao.module.stockTransfer.service.DlRepairStiService;
import cn.iocoder.yudao.module.stockTransfer.vo.DlRepairStiQueryVO;
import cn.iocoder.yudao.module.stockTransfer.vo.DlRepairStiReqVO;
import cn.iocoder.yudao.module.stockTransfer.vo.RepairSoiExportVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 调拨单子表(DlRepairSti)表控制层
 * @author 小李
 * @date 17:27 2024/9/13
**/
@RestController
@RequestMapping("/repair/sti")
public class DlRepairStiController{
    /**
     * 服务对象
     */
    @Resource
    private DlRepairStiService dlRepairStiService;

    @Resource
    @Lazy
    private CompanyService companyService;

    /**
     * 调拨单子表 分页
     *
     * @author 小李
     * @date 14:48 2024/9/19
     * @param repairStiReqVO 查询对象
     * @param pageNo 页码
     * @param pageSize 条数
    **/
    @GetMapping("/page")
    @Operation(summary = "调拨单子表 分页")
    public CommonResult<?> getStiPage(DlRepairStiReqVO repairStiReqVO,
                                      @RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo,
                                      @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize){
        Page<DlRepairSti> page = new Page<>(pageNo, pageSize);
        return success(dlRepairStiService.getStiPage(repairStiReqVO, page));
    }

    /**
     * 调拨单子表 配件详情
     *
     * @author 小李
     * @date 18:33 2024/9/22
     * @param ids ids
    **/
    @GetMapping("/getWares")
    @Operation(summary = "调拨单子表 配件详情")
    public CommonResult<?> getStIsWares(@RequestParam("ids") List<String> ids){
        return success(dlRepairStiService.getStIsWares(ids));
    }

    /**
     * 导出数据
     *
     * @author 小李
     * @date 17:43 2024/12/3
     * @param repairStiReqVO 请求对象
    **/
    @GetMapping("/export")
    @Operation(summary = "导出数据")
    public void exportData(DlRepairStiReqVO repairStiReqVO, HttpServletResponse response)throws IOException {
        int pageNo = 1, pageSize = 1000;
        Page<DlRepairSti> page = new Page<>(pageNo, pageSize);
        List<DlRepairSti> allList = new ArrayList<>();
        do {
            IPage<DlRepairSti> stiPage = dlRepairStiService.getStiPage(repairStiReqVO, page);
            List<DlRepairSti> records = stiPage.getRecords();
            if (CollUtil.isEmpty(records)){
                break;
            }
            allList.addAll(records);
            pageNo++;
            page.setCurrent(pageNo);
        }while (true);
        if (CollUtil.isEmpty(allList)){
            throw exception0(500, "没有数据可以导出");
        }
        List<Company> companies = companyService.list();
        Map<String, String> companyMap = companies.stream().collect(Collectors.toMap(Company::getId, Company::getCorpName));
        List<String> ids = allList.stream().map(DlRepairSti::getId).collect(Collectors.toList());
        List<DlRepairStiQueryVO> stIsWares = dlRepairStiService.getStIsWares(ids);
        List<RepairSoiExportVO> list = stIsWares.stream().map(item -> {
            RepairSoiExportVO repairSoiExportVO = new RepairSoiExportVO();
            DlRepairSt st = item.getSt();
            RepairWares wares = item.getWares();
            repairSoiExportVO.setStNo(st.getStNo());
            repairSoiExportVO.setName(wares.getName());
            repairSoiExportVO.setModel(wares.getModel());
            repairSoiExportVO.setCode(wares.getCode());
            repairSoiExportVO.setBarCode(wares.getBarCode());
            repairSoiExportVO.setGoodsCount(item.getGoodsCount());
            repairSoiExportVO.setPrice(wares.getPrice());
            int goodsCount = ObjectUtil.isNotEmpty(item.getGoodsCount()) ? item.getGoodsCount() : 0;
            BigDecimal price = ObjectUtil.isNotEmpty(wares.getPrice()) ? wares.getPrice() : BigDecimal.ZERO;
            repairSoiExportVO.setTotalPrice(price.multiply(BigDecimal.valueOf(goodsCount)));
            repairSoiExportVO.setGoodsPrice(item.getGoodsPrice());
            repairSoiExportVO.setUserName(st.getUserName());
            if (ObjectUtil.isNotEmpty(st.getInCorpId()) && ObjectUtil.isNotEmpty(companyMap.get(st.getInCorpId()))) {
                repairSoiExportVO.setInCorpId(companyMap.get(st.getInCorpId()));
            }
            if (ObjectUtil.isNotEmpty(st.getOutCorpId()) && ObjectUtil.isNotEmpty(companyMap.get(st.getOutCorpId()))) {
                repairSoiExportVO.setOutCorpId(companyMap.get(st.getOutCorpId()));
            }
            repairSoiExportVO.setStTime(st.getStTime());
            repairSoiExportVO.setRemark(item.getRemark());
            return repairSoiExportVO;
        }).collect(Collectors.toList());
        ExcelUtils.write(response, "调拨详细数据.xls", "数据", RepairSoiExportVO.class, list);
    }
}

