package com.bonus.material.clz.controller;

import cn.hutool.core.convert.Convert;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.bonus.common.biz.config.ListPagingUtil;
import com.bonus.common.core.utils.ServletUtils;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.clz.domain.back.MaterialBackApplyTotalInfo;
import com.bonus.material.clz.domain.back.MaterialBackApplyDetails;
import com.bonus.material.clz.domain.back.MaterialBackApplyInfo;
import com.bonus.material.clz.domain.vo.back.MaterialBackApplyRequestVo;
import com.bonus.material.clz.domain.vo.back.MaterialBackMaCodeVo;
import com.bonus.material.clz.service.MaterialBackApplyInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author ma_sh
 * @create 2025/6/25 9:30
 */
@Api(tags = "材料站退料任务接口")
@RestController
@RequestMapping("/material_back_apply_info")
public class MaterialBackApplyInfoController extends BaseController {

    @Resource
    private MaterialBackApplyInfoService materialBackApplyInfoService;

    /**
     * 查询退料任务列表
     */
    @ApiOperation(value = "查询退料任务列表")
    @GetMapping("/list")
    public AjaxResult list(MaterialBackApplyInfo backApplyInfo) {
        backApplyInfo.setIsExport(false);
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<MaterialBackApplyInfo> list = materialBackApplyInfoService.selectBackApplyInfoList(backApplyInfo);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 查询总站点退料记录数据
     * @param backApplyInfo
     * @return
     */
    @ApiOperation(value = "查询总站点退料记录数据")
    @GetMapping("/getTotalList")
    public AjaxResult getTotalList(MaterialBackApplyInfo backApplyInfo) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<MaterialBackApplyInfo> list = materialBackApplyInfoService.getTotalList(backApplyInfo);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 查询站点退料详情数据
     * @param backApplyInfo
     * @return
     */
    @ApiOperation(value = "查询总站点退料详情数据")
    @GetMapping("/getDetailsList")
    public AjaxResult getDetailsList(MaterialBackApplyInfo backApplyInfo) {
        startPage();
        List<MaterialBackApplyInfo> list = materialBackApplyInfoService.getDetailsList(backApplyInfo);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 获取退料任务详细信息
     */
    @ApiOperation(value = "获取退料任务详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,
                              @RequestParam(value = "keyWord", required = false) String keyWord) {
        return materialBackApplyInfoService.selectBackApplyInfoById(id, keyWord);
    }

    /**
     * 查询退料任务详情信息
     * @param backApplyDetails
     * @return
     */
    @ApiOperation(value = "查询退料任务详情信息")
    @GetMapping(value = "/getDetailsById")
    public AjaxResult getDetailsById(MaterialBackApplyDetails backApplyDetails) {
        return success(materialBackApplyInfoService.getDetailsById(backApplyDetails));
    }

    /**
     * 新增退料任务
     */
    @ApiOperation(value = "新增退料任务")
    @PreventRepeatSubmit
    @PostMapping
    public AjaxResult add(@Valid @RequestBody MaterialBackApplyRequestVo dto) {
        try {
            return materialBackApplyInfoService.insertBackApplyInfo(dto);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改退料任务
     */
    @ApiOperation(value = "修改退料任务")
    @PreventRepeatSubmit
    @SysLog(title = "退料任务", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改退料任务")
    @PostMapping("/edit")
    public AjaxResult edit(@Valid @RequestBody MaterialBackApplyRequestVo dto) {
        try {
            return materialBackApplyInfoService.updateBackApplyInfo(dto);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    @ApiOperation(value = "修改退料任务电子签名")
    @PreventRepeatSubmit
    @SysLog(title = "退料任务电子签名", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改退料任务电子签名")
    @PostMapping("/updateSignById")
    public AjaxResult updateSignById(@RequestBody MaterialBackApplyInfo dto) {
        try {
            return toAjax(materialBackApplyInfoService.updateSignById(dto));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 删除退料任务
     */
    @ApiOperation(value = "删除退料任务")
    @PreventRepeatSubmit
    @SysLog(title = "退料任务", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除退料任务")
    @PostMapping("/deleteById")
    public AjaxResult remove(@RequestBody MaterialBackApplyInfo backApplyInfo) {
        return materialBackApplyInfoService.deleteBackApplyInfoById(backApplyInfo.getId());
    }

    /**
     * 工器具退料记录
     * @param info
     * @return
     */
    @ApiOperation(value = "工器具退料记录")
    @GetMapping("/getBackInfoDetails")
    public AjaxResult getBackInfoDetails(MaterialBackApplyTotalInfo info) {
        info.setIsExport(1);
        startPage();
        List<MaterialBackApplyTotalInfo> list = materialBackApplyInfoService.getBackInfoDetails(info);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 工器具退料记录不带分页
     * @param info
     * @return
     */
    @ApiOperation(value = "工器具退料记录不带分页")
    @GetMapping("/getBackInfoDetailsNoPageList")
    public AjaxResult getBackInfoDetailsNoPageList(MaterialBackApplyTotalInfo info) {
        info.setIsExport(0);
        MaterialBackApplyTotalInfo backApplyTotalInfo =  new MaterialBackApplyTotalInfo();
        List<MaterialBackApplyTotalInfo> list = materialBackApplyInfoService.getBackInfoDetails(info);
        if (CollectionUtils.isNotEmpty(list)) {
            backApplyTotalInfo = list.get(0);
        }
        return AjaxResult.success(backApplyTotalInfo);
    }

    /**
     * 导出工器具退料记录查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出工器具退料记录查询")
    @PostMapping("/exportBackInfoDetails")
    public void exportBackInfoDetails(HttpServletResponse response, MaterialBackApplyTotalInfo bean) {
        bean.setIsExport(0);
        List<MaterialBackApplyTotalInfo> list = materialBackApplyInfoService.getBackInfoDetails(bean);
        ExcelUtil<MaterialBackApplyTotalInfo> util = new ExcelUtil<>(MaterialBackApplyTotalInfo.class);
        util.exportExcel(response, list, "综合查询--工器具退料记录查询");
    }

    /**
     * 工器具退料二级页面记录
     * @param info
     * @return
     */
    @ApiOperation(value = "工器具退料二级页面记录")
    @GetMapping("/getSecondBackInfo")
    public AjaxResult getSecondBackInfo(MaterialBackApplyTotalInfo info) {
        startPage();
        List<MaterialBackMaCodeVo> list = materialBackApplyInfoService.getSecondBackInfo(info);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出工器具退料二级页面查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出工器具退料二级页面查询")
    @PostMapping("/exportSecondBackInfo")
    public void exportSecondBackInfo(HttpServletResponse response, MaterialBackApplyTotalInfo bean) {
        List<MaterialBackMaCodeVo> list = materialBackApplyInfoService.getSecondBackInfo(bean);
        ExcelUtil<MaterialBackMaCodeVo> util = new ExcelUtil<>(MaterialBackMaCodeVo.class);
        util.exportExcel(response, list, "综合查询--工器具退料二级页面查询");
    }

    @ApiOperation(value = "在用设备类型树")
    @PostMapping("/getUseTypeTree")
    public AjaxResult getUseTypeTree(@RequestBody MaterialBackApplyInfo bean) {
        return materialBackApplyInfoService.getUseTypeTree(bean);
    }

    /**
     * 根据班组和工程id查询领料机具
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据班组和工程id查询领料机具")
    @GetMapping("/getMachineById")
    public AjaxResult getMachineById(MaterialBackApplyInfo dto) {
        return materialBackApplyInfoService.getMachineById(dto);
    }

    /**
     * 退料申请提交
     */
    @ApiOperation(value = "退料申请提交")
    @PreventRepeatSubmit(interval = 2)
    @SysLog(title = "退料任务", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->退料申请提交")
    @PostMapping("/submitBackApply")
    public AjaxResult submitBackApply(@RequestBody MaterialBackApplyInfo backApplyInfo) {
        try {
            return materialBackApplyInfoService.submitBackApply(backApplyInfo);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * app编码检索
     * @param dto
     * @return
     */
    @ApiOperation(value = "app编码检索")
    @GetMapping("/getMachine")
    public AjaxResult getMachine(MaterialBackApplyInfo dto) {
        return materialBackApplyInfoService.getMachine(dto);
    }
}
