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

import cn.estsh.i3plus.ext.mes.api.base.IPartAutoLandedService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOrderRule;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartAutoLanded;
import cn.estsh.i3plus.ext.mes.pojo.model.ExcelImportResultExtModel;
import cn.estsh.i3plus.ext.mes.pojo.model.ExcelPartAutoLanded;
import cn.estsh.i3plus.ext.mes.pojo.model.ExcelWorkOrderDto;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
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.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesBaseController;
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/1/27 16:54
 **/
@RestController
@Api(tags = "自动发布物料配置")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/part-auto-landed")
public class PartAutoLandedController extends MesBaseController {
    @Autowired
    private IPartAutoLandedService partAutoLandedService;

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

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

    /**
     * 修改单据规则
     *
     * @param mesPartAutoLanded mes部分自动降落
     * @return {@link ResultBean}
     */
    @PutMapping(value = "/update")
    @ApiOperation(value = "修改")
    public ResultBean update(@RequestBody MesPartAutoLanded mesPartAutoLanded) {
        try {
            //条件验证
            ValidatorBean.beginValid(mesPartAutoLanded)
                    .notNull("partNo", mesPartAutoLanded.getPartNo())
                    .notNull("workCenterCode", mesPartAutoLanded.getWorkCenterCode());
            return ResultBean.success("修改成功").setResultObject(partAutoLandedService.update(mesPartAutoLanded))
                    .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(partAutoLandedService.updateIsValidById(id,status))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 查询分页
     *
     * @param mesPartAutoLanded mes自动
     * @param pager             寻呼机
     * @return {@link ResultBean}
     */
    @GetMapping(value = "/query")
    @ApiOperation(value = "查询分页")
    public ResultBean queryOrderRule(MesPartAutoLanded mesPartAutoLanded, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(partAutoLandedService.queryByPager(mesPartAutoLanded, 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);

            if (!file.getOriginalFilename().contains(".xlsx")) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.BUSINESS_EXCEPTION_DATA_ERROR.getCode())
                        .setErrorDetail("格式错误，请检查格式后再次导入")
                        .setErrorSolution("格式错误，请检查格式后再次导入")
                        .build();
            }
            List<ExcelPartAutoLanded> excelPartAutoLandeds = excelTool.importData(file.getOriginalFilename(), file.getInputStream(), ExcelPartAutoLanded.class);
            partAutoLandedService.importData(excelPartAutoLandeds);
            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);
        }

    }
}
