package cn.estsh.i3plus.ext.mes.apiservice.controller.base;

import cn.estsh.i3plus.ext.mes.api.base.IFailCaseCheckService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesFailCaseCheck;
import cn.estsh.i3plus.ext.mes.pojo.model.ExcelFailCaseCheck;
import cn.estsh.i3plus.ext.mes.pojo.model.ExcelImportResultExtModel;
import cn.estsh.i3plus.platform.common.tool.ExcelTool;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import java.util.List;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/2 15:51
 **/
@RestController
@Api(tags = "失败事例点检")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/fail-case-check")
public class FailCaseCheckController {

    @Autowired
    private IFailCaseCheckService failCaseCheckService;

    @Resource(name = "redisRes")
    private ImppRedis redisRes;
    @Autowired
    private EntityManager entityManager;

    /**
     * 插入
     *
     * @param failCaseCheck mes自动
     * @return {@link ResultBean}
     */
    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增")
    public ResultBean insert(@RequestBody MesFailCaseCheck failCaseCheck) {
        try {
            //条件验证
            ValidatorBean.beginValid(failCaseCheck)
                    .notNull("processCode", failCaseCheck.getProcessCode())
                    .notNull("partNo", failCaseCheck.getPartNo())
            ;
            return ResultBean.success("新增成功").setResultObject(failCaseCheckService.insert(failCaseCheck))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 修改单据规则
     *
     * @param failCaseCheck mes部分自动降落
     * @return {@link ResultBean}
     */
    @PutMapping(value = "/update")
    @ApiOperation(value = "修改")
    public ResultBean update(@RequestBody MesFailCaseCheck failCaseCheck) {
        try {
            //条件验证
            ValidatorBean.beginValid(failCaseCheck)
                    .notNull("processCode", failCaseCheck.getProcessCode())
                    .notNull("partNo", failCaseCheck.getPartNo());
            return ResultBean.success("修改成功").setResultObject(failCaseCheckService.update(failCaseCheck))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 根据ID启用或禁用单据规则
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping(value = "/status-update")
    @ApiOperation(value = "根据ID启用或禁用")
    public ResultBean updateOrderIsValidById(long id, Integer status) {
        try {
            return ResultBean.success("根据ID启用或禁用").setResultObject(failCaseCheckService.updateIsValidById(id, status))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 查询分页
     *
     * @param failCaseCheck mes自动
     * @param pager         寻呼机
     * @return {@link ResultBean}
     */
    @GetMapping(value = "/query")
    @ApiOperation(value = "查询分页")
    public ResultBean queryOrderRule(MesFailCaseCheck failCaseCheck, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(failCaseCheckService.queryByPager(failCaseCheck, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 导入
     *
     * @param file 导入文件
     * @return
     */
    @ApiOperation("导入工单")
    @PostMapping("import")
    public ResultBean importWorkOrder(@RequestParam("file") MultipartFile file) {
        try {
            ExcelTool excelTool = new ExcelTool(entityManager, redisRes).setThrowAllErrorMode(true);

            List<ExcelFailCaseCheck> excelfailCaseChecks = excelTool.importData(file.getOriginalFilename(), file.getInputStream(), ExcelFailCaseCheck.class);
            failCaseCheckService.importData(excelfailCaseChecks);
            ExcelImportResultExtModel excelImportResultExtModel = new ExcelImportResultExtModel();
            excelImportResultExtModel.setResult(true);
            return ResultBean.success().setResultObject(excelImportResultExtModel);
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }

    }
}
