package cn.iocoder.yudao.module.stockOperate.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.conf.entity.BaseWarehouse;
import cn.iocoder.yudao.module.conf.service.BaseWarehouseService;
import cn.iocoder.yudao.module.project.entity.RepairWares;
import cn.iocoder.yudao.module.stockOperate.entity.DlRepairSo;
import cn.iocoder.yudao.module.stockOperate.entity.DlRepairSoi;
import cn.iocoder.yudao.module.stockOperate.service.DlRepairSoiService;
import cn.iocoder.yudao.module.stockOperate.vo.*;
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;

/**
 * 采购单领料单子表(DlRepairSoi)表控制层
 * @author 小李
 * @date 9:14 2024/9/13
**/
@RestController
@RequestMapping("/repair/soi")
public class DlRepairSoiController{
    /**
     * 服务对象
     */
    @Resource
    private DlRepairSoiService dlRepairSoiService;

    @Resource
    @Lazy
    private BaseWarehouseService warehouseService;

    /**
     * 采购单领料单子表 分页
     *
     * @author 小李
     * @date 10:44 2024/9/18
     * @param repairSoiReqVO 查询对象
     * @param pageNo 页码
     * @param pageSize 条数
    **/
    @GetMapping("/page")
    @Operation(summary = "采购单领料单子表 分页")
    public CommonResult<?> getRepairSoiPage(DlRepairSoiReqVO repairSoiReqVO,
                                            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<DlRepairSoi> page = new Page<>(pageNo, pageSize);
        return success(dlRepairSoiService.getRepairSoiPage(repairSoiReqVO, page));
    }

    /**
     * 采购单领料单子表详细 按ID查
     *
     * @author 小李
     * @date 11:24 2024/9/22
     * @param ids ids
    **/
    @GetMapping("/get")
    @Operation(summary = "采购单领料单子表详细 按ID查")
    public CommonResult<?> getRepairSoiByIds(@RequestParam(value = "ids") List<String> ids){
        return success(dlRepairSoiService.getRepairSoiByIds(ids));
    }

    /**
     * 按主表ID查
     *
     * @author 小李
     * @date 11:22 2024/10/21
     * @param soId 主表ID
    **/
    @GetMapping("/getBySoId")
    @Operation(summary = "按主表ID查")
    public CommonResult<?> getRepairSoiBySoId(@RequestParam(value = "soId") String soId){
        return success(dlRepairSoiService.getRepairSoiBySoId(soId));
    }

    /**
     * 根据主表的ID得到子表的所有数据，按类型区分的map，支持条件查询
     *
     * @author 小李
     * @date 15:51 2024/11/19
     * @param id id
     **/
    @GetMapping("/getMapBySoIdAndQuery")
    @Operation(summary = "根据主表的ID得到子表的所有数据，按类型区分的map，支持条件查询")
    public CommonResult<?> getMapBySoIdAndQuery(@RequestParam("id") String id, @RequestParam("query") String query) {
        return success(dlRepairSoiService.getMapBySoIdAndQuery(id, query));
    }

    /**
     * 根据供应商查询该供应商采购过的配件 分页
     *
     * @author 小李
     * @date 11:49 2024/11/28
     * @param reqVO 查询对象
     * @param pageNo 页码
     * @param pageSize 条数
    **/
    @GetMapping("/getSoBySupplier")
    @Operation(summary = "根据供应商查询该供应商采购过的配件 分页")
    public CommonResult<?> getSoBySupplier(DlRepairSoiReqVO reqVO,
                                           @RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo,
                                           @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize){
        Page<DlRepairSoiRespVO> page = new Page<>(pageNo, pageSize);
        return success(dlRepairSoiService.getSoBySupplier(reqVO, page));
    }

    /**
     * 导出数据
     *
     * @author 小李
     * @date 16:30 2024/12/3
    **/
    @GetMapping("/export")
    @Operation(summary = "导出数据")
    public void exportData(DlRepairSoiReqVO repairSoiReqVO, HttpServletResponse response)throws IOException {
        int pageNo = 1, pageSize = 1000;
        Page<DlRepairSoi> page = new Page<>(pageNo, pageSize);
        List<DlRepairSoi> allList = new ArrayList<>();
        do {
            IPage<DlRepairSoi> repairSoiPage = dlRepairSoiService.getRepairSoiPage(repairSoiReqVO, page);
            List<DlRepairSoi> records = repairSoiPage.getRecords();
            if (CollUtil.isEmpty(records)){
                break;
            }
            allList.addAll(records);
            pageNo++;
            page.setCurrent(pageNo);
        }while (true);
        if (CollUtil.isEmpty(allList)){
            throw exception0(500, "没有数据可以导出");
        }
        List<String> ids = allList.stream().map(DlRepairSoi::getId).collect(Collectors.toList());
        List<DlRepairSoiQueryRespVO> repairSoiByIds = dlRepairSoiService.getRepairSoiByIds(ids);
        List<BaseWarehouse> warehouses = warehouseService.list();
        Map<String, String> warehousesMap = warehouses.stream().collect(Collectors.toMap(BaseWarehouse::getId, BaseWarehouse::getName));
        List<RepairSoiExportVO> list = repairSoiByIds.stream().map(item -> {
            DlRepairSo so = item.getSo();
            RepairWares repairWares = item.getRepairWares();
            RepairSoiExportVO repairSoiExportVO = new RepairSoiExportVO();
            repairSoiExportVO.setSoNo(so.getSoNo());
            repairSoiExportVO.setName(repairWares.getName());
            repairSoiExportVO.setUserName(so.getUserName());
            repairSoiExportVO.setCode(repairWares.getCode());
            repairSoiExportVO.setBarCode(repairWares.getBarCode());
            repairSoiExportVO.setModel(repairWares.getModel());
            repairSoiExportVO.setGoodsCount(item.getGoodsCount());
            repairSoiExportVO.setGoodsPrice(item.getGoodsPrice());
            BigDecimal goodsPrice = ObjectUtil.isNotEmpty(item.getGoodsPrice()) ? item.getGoodsPrice() : BigDecimal.ZERO;
            int goodsCount = ObjectUtil.isNotEmpty(item.getGoodsCount()) ? item.getGoodsCount() : 0;
            repairSoiExportVO.setTotal(goodsPrice.multiply(BigDecimal.valueOf(goodsCount)));
            repairSoiExportVO.setSupplierName(so.getSupplierName());
            if (ObjectUtil.isNotEmpty(item.getWareId())) {
                repairSoiExportVO.setWarehouseName(warehousesMap.get(item.getWareId()));
            }
            repairSoiExportVO.setCropId(so.getCorpId());
            repairSoiExportVO.setCreateTime(so.getCreateTime());
            repairSoiExportVO.setRemark(item.getRemark());
            return repairSoiExportVO;
        }).collect(Collectors.toList());
        ExcelUtils.write(response, "作废单据数据.xls", "数据", RepairSoiExportVO.class, list);
    }
}

