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.common.util.object.BeanUtils;
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.stockTransfer.entity.DlRepairSt;
import cn.iocoder.yudao.module.stockTransfer.service.DlRepairStService;
import cn.iocoder.yudao.module.stockTransfer.vo.DlRepairStReqVO;
import cn.iocoder.yudao.module.stockTransfer.vo.DlRepairStRespVO;
import cn.iocoder.yudao.module.stockTransfer.vo.RepairStExportVO;
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.*;

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

import java.io.IOException;
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;

/**
 * 调拨单(DlRepairSt)表控制层
 * @author 小李
 * @date 17:26 2024/9/13
**/
@RestController
@RequestMapping("/repair/st")
public class DlRepairStController{
    /**
     * 服务对象
     */
    @Resource
    private DlRepairStService dlRepairStService;

    @Resource
    @Lazy
    private CompanyService companyService;

    /**
     * 调拨单 新增
     *
     * @author 小李
     * @date 19:06 2024/9/18
     * @param repairStRespVO 新增对象
    **/
    @PostMapping("/create")
    @Operation(summary = "调拨单 新增")
    public CommonResult<?> createSt(@RequestBody DlRepairStRespVO repairStRespVO){
        dlRepairStService.createSt(repairStRespVO);
        return CommonResult.ok();
    }

    /**
     * 调拨单（出库、入库） 分页
     *
     * @author 小李
     * @date 9:20 2024/9/19
     * @param repairStReqVO 查询对象
     * @param pageNo 页码
     * @param pageSize 条数
    **/
    @GetMapping("/page")
    @Operation(summary = "调拨单（出库、入库） 分页")
    public CommonResult<?> getStPage(DlRepairStReqVO repairStReqVO,
                                     @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<DlRepairSt> page = new Page<>(pageNo, pageSize);
        return success(dlRepairStService.getStPage(repairStReqVO, page));
    }

    /**
     * 调拨单（出库、入库） 作废
     *
     * @author 小李
     * @date 15:11 2024/9/22
     * @param repairStReqVO 请求对象
    **/
    @PostMapping("/void")
    @Operation(summary = "调拨单（出库、入库） 作废")
    public CommonResult<?> voidSt(@RequestBody DlRepairStReqVO repairStReqVO){
        dlRepairStService.voidSt(repairStReqVO);
        return CommonResult.ok();
    }

    /**
     * 调拨单（出库、入库） 详情查询
     *
     * @author 小李
     * @date 15:52 2024/9/22
     * @param id 查询ID
    **/
    @GetMapping("/get")
    @Operation(summary = "调拨单（出库、入库） 详情查询")
    public CommonResult<?> getSt(@RequestParam("id") String id){
        return success(dlRepairStService.getSt(id));
    }

    /**
     * 导出数据
     *
     * @author 小李
     * @date 17:26 2024/12/3
     * @param repairStReqVO 请求对象
    **/
    @GetMapping("/export")
    @Operation(summary = "导出数据")
    public void exportData(DlRepairStReqVO repairStReqVO, HttpServletResponse response)throws IOException {
        int pageNo = 1, pageSize = 1000;
        List<RepairStExportVO> list = new ArrayList<>();
        Page<DlRepairSt> page = new Page<>(pageNo, pageSize);
        do {
            IPage<DlRepairSt> stPage = dlRepairStService.getStPage(repairStReqVO, page);
            List<DlRepairSt> records = stPage.getRecords();
            if (CollUtil.isEmpty(records)){
                break;
            }
            List<RepairStExportVO> convertedRecords = records.stream().map(item -> BeanUtils.toBean(item, RepairStExportVO.class)).collect(Collectors.toList());
            list.addAll(convertedRecords);
            pageNo++;
            page.setCurrent(pageNo);
        }while (true);
        if (CollUtil.isEmpty(list)){
            throw exception0(500, "没有数据可以导出");
        }
        List<Company> companies = companyService.list();
        Map<String, String> companyMap = companies.stream().collect(Collectors.toMap(Company::getId, Company::getCorpName));
        list.forEach(item -> {
            if (ObjectUtil.isNotEmpty(item.getInCorpId()) && ObjectUtil.isNotEmpty(companyMap.get(item.getInCorpId()))){
                item.setInCorpId(companyMap.get(item.getInCorpId()));
            }
            if (ObjectUtil.isNotEmpty(item.getOutCorpId()) && ObjectUtil.isNotEmpty(companyMap.get(item.getOutCorpId()))){
                item.setOutCorpId(companyMap.get(item.getOutCorpId()));
            }
        });
        ExcelUtils.write(response, "单据数据.xls", "数据", RepairStExportVO.class, list);
    }
}

