package com.steerinfo.ems.emsprodplanweightadjustment.controller;

import com.alibaba.fastjson.JSON;
import com.steerinfo.auth.utils.JwtUtil;
import com.steerinfo.ems.websocket.controller.WebSocket;
import com.steerinfo.ems.emsgmpcjh.mapper.EmsGmPcJhMapper;
import com.steerinfo.ems.emsgmpcjh.model.EmsGmPcJh;
import com.steerinfo.ems.emsprodplanweightadjustment.mapper.EmsProdplanWeightAdjustmentMapper;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.ems.emsprodplanweightadjustment.model.EmsProdplanWeightAdjustment;
import com.steerinfo.ems.emsprodplanweightadjustment.service.IEmsProdplanWeightAdjustmentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * EmsProdplanWeightAdjustment RESTful接口：
 * @author generator
 * @version 1.0-SNAPSHORT 2021-09-22 05:40
 * 类描述
 * 修订历史：
 * 日期：2021-09-22
 * 作者：generator
 * 参考：
 * 描述：EmsProdplanWeightAdjustment RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/emsprodplanweightadjustments")
public class EmsProdplanWeightAdjustmentController extends BaseRESTfulController {

    @Autowired
    IEmsProdplanWeightAdjustmentService emsProdplanWeightAdjustmentService;
    @Autowired
    EmsProdplanWeightAdjustmentMapper emsProdplanWeightAdjustmentMapper;
    @Autowired
    EmsGmPcJhMapper emsGmPcJhMapper;
    @Autowired
    private WebSocket webSocket;
    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("emsprodplanweightadjustment:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<EmsProdplanWeightAdjustment> list = emsProdplanWeightAdjustmentService.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="获取列表", notes="分页模糊查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("emsprodplanweightadjustment:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<EmsProdplanWeightAdjustment> list = emsProdplanWeightAdjustmentService.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }
    
    @ApiOperation(value="创建", notes="根据EmsProdplanWeightAdjustment对象创建")
    @ApiImplicitParam(name = "emsProdplanWeightAdjustment", value = "详细实体emsProdplanWeightAdjustment", required = true, dataType = "EmsProdplanWeightAdjustment")
    //@RequiresPermissions("emsprodplanweightadjustment:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute EmsProdplanWeightAdjustment model){
        EmsProdplanWeightAdjustment emsProdplanWeightAdjustment = emsProdplanWeightAdjustmentService.add(model);
        return success(emsProdplanWeightAdjustment);
    }

    @ApiOperation(value="获取详细信息", notes="根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("emsprodplanweightadjustment:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id){
        EmsProdplanWeightAdjustment emsProdplanWeightAdjustment = emsProdplanWeightAdjustmentService.getById(id);
        return success(emsProdplanWeightAdjustment);
    }

    @ApiOperation(value="更新详细信息", notes="根据url的id来指定更新对象，并根据传过来的emsProdplanWeightAdjustment信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
        @ApiImplicitParam(name = "emsProdplanWeightAdjustment", value = "详细实体emsProdplanWeightAdjustment", required = true, dataType = "EmsProdplanWeightAdjustment")
    })
    //@RequiresPermissions("emsprodplanweightadjustment:update")
    @PutMapping(value = "/{id}", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody EmsProdplanWeightAdjustment model){
        model.setId(id);
        EmsProdplanWeightAdjustment emsProdplanWeightAdjustment = emsProdplanWeightAdjustmentService.modify(model);
        return success(emsProdplanWeightAdjustment);
    }

    @ApiOperation(value="删除", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("emsprodplanweightadjustment:delete")
    @DeleteMapping(value = "/{id}")//String
    public RESTfulResult delete(@PathVariable String id){
    	List<String> list = Arrays.asList(id.split(","));
    	if(ListUtils.isNotEmpty(list)) {
	    	List<String> ids = ListUtils.convertList(list);
			  emsProdplanWeightAdjustmentService.delete(ids);
    	}
      return success();
    }
    @ApiOperation(value="更新详细信息", notes="根据url的id来指定更新对象，并根据传过来的emsProdplanWeightAdjustment信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emsProdplanWeightAdjustment", value = "详细实体emsProdplanWeightAdjustment", required = true, dataType = "EmsProdplanWeightAdjustment")
    })
    @PutMapping(value = "/submitaudit",produces = "application/json;charset=UTF-8")
    public RESTfulResult submitaudit(@RequestBody EmsProdplanWeightAdjustment[] models){
        String userId = JwtUtil.getUseridByToken();
        String soketMessage="国贸订单编号";
        boolean result = false;
        EmsProdplanWeightAdjustment emsProdplanWeightAdjustment;
        for (EmsProdplanWeightAdjustment model : models) {
            emsProdplanWeightAdjustment = emsProdplanWeightAdjustmentMapper.selectByPrimaryKey(model.getId());
            if (emsProdplanWeightAdjustment.getAuditStatus().equals("2")) {
                return failed(null,"编号为"+emsProdplanWeightAdjustment.getId()+",领导部门已经审批,请勿重复操作");
            }
            if (emsProdplanWeightAdjustment.getAuditStatus().equals("3")) {
                return failed(null,"编号为"+emsProdplanWeightAdjustment.getId()+",生产部已经同意,请勿重复操作");
            }
            emsProdplanWeightAdjustment.setSubmitter(userId);
            emsProdplanWeightAdjustment.setSubmitTime(new Date());
            emsProdplanWeightAdjustment.setAuditStatus(model.getAuditStatus());
            emsProdplanWeightAdjustment.setPlanWeight(model.getPlanWeight());
            emsProdplanWeightAdjustmentMapper.updateByPrimaryKey(emsProdplanWeightAdjustment);
            result = true;
            soketMessage+=model.getId()+",";
        }
        if(result){
            soketMessage += "已提交审核,请注意接收";
            String[] roles = {"生产部-生产处"};
            Map <String, Object> map = new HashMap<String,Object>();
            map.put("role",roles);
            map.put("title","销售订单调整");
            map.put("message",soketMessage);
            map.put("tips","您好，有新的销售订单需要进行重量调整,请注意接收。");
            soketMessage= JSON.toJSONString(map);
            webSocket.sendAllMessage(soketMessage);
        }
            return success(null,"提交成功");
    }
    @ApiOperation(value="更新详细信息", notes="根据url的id来指定更新对象，并根据传过来的emsProdplanWeightAdjustment信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emsProdplanWeightAdjustment", value = "详细实体emsProdplanWeightAdjustment", required = true, dataType = "EmsProdplanWeightAdjustment")
    })
    @PutMapping(value = "approval", produces  = "application/json;charset=UTF-8")
    public RESTfulResult approval(@RequestBody EmsProdplanWeightAdjustment[] models ){
        String userId = JwtUtil.getUseridByToken();
        for (EmsProdplanWeightAdjustment model : models) {
            EmsProdplanWeightAdjustment emsProdplanWeightAdjustment = emsProdplanWeightAdjustmentMapper.selectByPrimaryKey(model.getId());
            if (emsProdplanWeightAdjustment != null ) {
                if (emsProdplanWeightAdjustment.getAuditStatus().equals("2")) {
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",部门已经审批,请勿重复操作");
                }
                if (emsProdplanWeightAdjustment.getAuditStatus().equals("0")) {
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",先提交审核,请勿重复操作");
                }
                if (emsProdplanWeightAdjustment.getAuditStatus().equals("3")) {
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",生产部已经审批,请勿重复操作");
                }
                if(emsProdplanWeightAdjustment.getAuditStatus().equals("4")){
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",计划审核失败,请勿重复操作");
                }
                emsProdplanWeightAdjustment.setAuditStatus("2");
                emsProdplanWeightAdjustment.setGmReviewer(userId);
                emsProdplanWeightAdjustment.setGmReviewerTime(new Date());
                emsProdplanWeightAdjustmentMapper.updateByPrimaryKey(emsProdplanWeightAdjustment);
            }
        }
        return success();
    }

    @ApiOperation(value="更新详细信息", notes="根据url的id来指定更新对象，并根据传过来的emsProdplanWeightAdjustment信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emsProdplanWeightAdjustment", value = "详细实体emsProdplanWeightAdjustment", required = true, dataType = "EmsProdplanWeightAdjustment")
    })
    @PutMapping(value = "nopass", produces  = "application/json;charset=UTF-8")
    public RESTfulResult nopass(@RequestBody EmsProdplanWeightAdjustment[] models ){
        String userId = JwtUtil.getUseridByToken();
        String soketMessage="国贸订单编号";
        boolean result = false;
        for (EmsProdplanWeightAdjustment model : models) {
            EmsProdplanWeightAdjustment emsProdplanWeightAdjustment = emsProdplanWeightAdjustmentMapper.selectByPrimaryKey(model.getId());
            if (emsProdplanWeightAdjustment != null ) {
                if (emsProdplanWeightAdjustment.getAuditStatus().equals("3")) {
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",生产部已经审批,请勿重复操作");
                }
                if(emsProdplanWeightAdjustment.getAuditStatus().equals("4")){
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",计划审核失败,请勿重复操作");
                }
                emsProdplanWeightAdjustment.setGmReviewer(userId);
                emsProdplanWeightAdjustment.setGmReviewerTime(new Date());
                emsProdplanWeightAdjustment.setAuditStatus("0");
                emsProdplanWeightAdjustmentMapper.updateByPrimaryKey(emsProdplanWeightAdjustment);
                result = true;
            }
            soketMessage+=model.getId()+",";
            if(result){
                soketMessage += "不通过,请注意查看";
                String[] roles = {"国贸-业务人员"};
                Map <String, Object> map = new HashMap<String,Object>();
                map.put("role",roles);
                map.put("title","销售订单重量调整");
                map.put("message",soketMessage);
                map.put("tips","您好，有提交的订单不通过，请注意查看。");
                soketMessage= JSON.toJSONString(map);
                webSocket.sendAllMessage(soketMessage);
            }
        }
                 return success();
    }

    @PutMapping(value = "agreement", produces  = "application/json;charset=UTF-8")
    public RESTfulResult agreement(@RequestBody EmsProdplanWeightAdjustment[] models){
        String userId = JwtUtil.getUseridByToken();
//        String soketMessage="国贸订单编号";
//        boolean result = false;
        for (EmsProdplanWeightAdjustment model : models) {
            EmsProdplanWeightAdjustment emsProdplanWeightAdjustment = emsProdplanWeightAdjustmentMapper.selectByPrimaryKey(model.getId());
            if (emsProdplanWeightAdjustment != null ) {
                if (emsProdplanWeightAdjustment.getAuditStatus().equals("0")) {
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",还未走审批流程,操作失败");
                }
                if (emsProdplanWeightAdjustment.getAuditStatus().equals("1")) {
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",领导部门还未审批,操作失败");
                }
                if (emsProdplanWeightAdjustment.getAuditStatus().equals("3")) {
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",该计划生产部已经审批,请勿重复操作");
                }
                if(emsProdplanWeightAdjustment.getAuditStatus().equals("4")){
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",审核失败,请勿重复操作");
                }
                EmsGmPcJh emsGmPcJh = emsGmPcJhMapper.selectByPrimaryKey(model.getId());
                emsProdplanWeightAdjustment.setReviewer(userId);
                emsProdplanWeightAdjustment.setReviewerTime(new Date());
                emsProdplanWeightAdjustment.setRecordWeight(emsGmPcJh.getPlanWeight());
                emsProdplanWeightAdjustment.setAuditStatus("3");
                emsProdplanWeightAdjustmentMapper.updateByPrimaryKey(emsProdplanWeightAdjustment);
                emsGmPcJh.setPlanWeight(emsProdplanWeightAdjustment.getPlanWeight());
                emsGmPcJhMapper.updateByPrimaryKey(emsGmPcJh);
//                result = true;
//                soketMessage+=model.getId()+",";
//                if(result){
//                    soketMessage += "已通过,请注意查看";
//                    String[] roles = {"国贸-部门领导"};
//                    Map <String, Object> map = new HashMap<String,Object>();
//                    map.put("role",roles);
//                    map.put("title","销售订单重量调整");
//                    map.put("message",soketMessage);
//                    map.put("tips","您好，有提交的订单不通过，请注意查看。");
//                    soketMessage= JSON.toJSONString(map);
//                    webSocket.sendAllMessage(soketMessage);
//                }
            }

        }

            return success();
    }
    @PutMapping(value = "rejected", produces  = "application/json;charset=UTF-8")
    public RESTfulResult rejected(@RequestBody EmsProdplanWeightAdjustment[] models){
        String userId = JwtUtil.getUseridByToken();
        for (EmsProdplanWeightAdjustment model : models) {
            EmsProdplanWeightAdjustment emsProdplanWeightAdjustment = emsProdplanWeightAdjustmentMapper.selectByPrimaryKey(model.getId());
            if (emsProdplanWeightAdjustment != null ) {
                if (emsProdplanWeightAdjustment.getAuditStatus().equals("0")) {
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",还未走审批流程,操作失败");
                }
                if (emsProdplanWeightAdjustment.getAuditStatus().equals("1")) {
                    return failed(null, "计划编号为:"+emsProdplanWeightAdjustment.getId()+",领导部门还未审批,操作失败");
                }
                if(emsProdplanWeightAdjustment.getAuditStatus().equals("4")){
                    return failed(null,"计划编号为:"+emsProdplanWeightAdjustment.getId()+",审核失败,请勿重复操作");
                }
                EmsGmPcJh emsGmPcJh = emsGmPcJhMapper.selectByPrimaryKey(model.getId());
                emsGmPcJh.setPlanWeight(emsProdplanWeightAdjustment.getRecordWeight());
                emsGmPcJhMapper.updateByPrimaryKey(emsGmPcJh);
                emsProdplanWeightAdjustment.setReviewer(userId);
                emsProdplanWeightAdjustment.setReviewerTime(new Date());
                emsProdplanWeightAdjustment.setAuditStatus("2");
                emsProdplanWeightAdjustmentMapper.updateByPrimaryKey(emsProdplanWeightAdjustment);
            }
        }
        return success();
    }

}
