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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesOrderJobService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOrderJob;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesOrderJobModel;
import cn.estsh.i3plus.mes.pcn.api.iservice.base.IShiftService;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
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.i3plus.pojo.mes.bean.MesShift;
import cn.estsh.impp.framework.base.controller.MesPcnBaseController;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/22 16:14
 * @Description:
 **/
@RestController
@Api(tags = "单据作业")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/order-job")
public class OrderJobController extends MesPcnBaseController {
    @Autowired
    private IMesOrderJobService orderJobService;

    @Autowired
    private IShiftService shiftService;


    /**
     * 修改单据作业状态
     *
     * @param id
     * @param orderStatus
     * @return
     */
    @PutMapping(value = "/mes-order-job/order-status/update")
    @ApiOperation(value = "修改单据作业状态")
    public ResultBean orderJobUpdateOrderStatus(long id, Integer orderStatus, String organizeCode, String modifyUser) {
        try {
            return ResultBean.success("修改成功").setResultObject(orderJobService.orderJobUpdateOrderStatus(id, orderStatus, organizeCode, modifyUser))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/mes-order-job/update-status-batch")
    @ApiOperation(value = "根据id批量修改单据状态", notes = "根据id批量修改单据状态")
    public ResultBean updateProcessCheckValidByIds(String[] idsStr, Integer orderStatus, String organizeCode, String modifyUser) {
        try {
            ConvertBean.modelSafeArrayNumber(idsStr, true);
            orderJobService.updateOrderStatusByIds(idsStr, orderStatus, organizeCode, modifyUser);
            return ResultBean.success("修改成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    /**
     * 按条件查找创建、点检中的单据作业
     *
     * @param orderJob
     * @return
     */
    @GetMapping(value = "/mes-order-job/query")
    @ApiOperation(value = "按条件查找创建、点检中的单据作业")
    public ResultBean queryMesOrderJob(MesOrderJobModel orderJob) {
        try {
            return ResultBean.success("查询成功").setResultList(orderJobService.queryMesOrderJob(orderJob))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 按条件查找创建、点检中的单据作业
     *
     * @param orderJob
     * @param pager
     * @return
     */
    @GetMapping(value = "/mes-order-job/by-pager/query")
    @ApiOperation(value = "按条件分页查找创建、点检中的单据作业")
    public ResultBean queryMesOrderJobByPager(MesOrderJobModel orderJob, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(orderJobService.queryMesOrderJobByPager(orderJob, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 查询产线当前班次
     *
     * @param workCenterCode
     * @param organizeCode
     * @return
     */
    @GetMapping(value = "/work_center/query")
    @ApiOperation(value = "查询产线当前班次")
    public ResultBean getWorkCenter(String workCenterCode, String organizeCode) {
        try {
            return ResultBean.success("查询成功").setResultObject(orderJobService.getWorkCenter(workCenterCode, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @GetMapping({"/mes-shift-all/query"})
    @ApiOperation("按条件查询班次所有有效数据")
    public ResultBean queryMesShiftAll(MesShift shift) {
        try {
            shift.setOrganizeCode(shift.getOrganizeCode());
            List<MesShift> shiftList = this.shiftService.queryMesShiftAll(shift);
            shiftList = shiftList.stream().collect(
                    collectingAndThen(
                            toCollection(() -> new TreeSet<>(comparing(n -> n.getShiftCode()))), ArrayList::new)
            );
            return ResultBean.success("操作成功").setResultList(shiftList).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException var3) {
            return ResultBean.fail(var3);
        } catch (Exception var4) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(var4);
        }
    }


    @PutMapping(value = "/commit")
    @ApiOperation(value = "提交单据")
    public ResultBean doCommitOrderJob(String orderNo, Integer orderType, String userName, String organizeCode) {
        try {
            orderJobService.doCommitOrderJob(orderNo, orderType, userName, organizeCode);
            return ResultBean.success("操作成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
