package com.prolog.cs.base.gsp.controller;


import com.prolog.cs.base.gsp.QueryDTO;
import com.prolog.cs.base.gsp.model.GspUpkeepCheck;
import com.prolog.cs.base.gsp.model.GspUpkeepCheckPlan;
import com.prolog.cs.base.gsp.service.GspUpkeepCheckPlanService;
import com.prolog.cs.base.gsp.service.GspUpkeepCheckService;
import com.prolog.cs.base.gsp.service.impl.AutoUpKeepTask;
import com.prolog.cs.base.gsp.vo.GspUpkeepCheckVO;
import com.prolog.cs.base.gsp.vo.InfTaskOutBackDTO;
import com.prolog.cs.data.exchange.client.ExchangeSender;
import com.prolog.cs.data.exchange.client.core.EtlMessage;
import com.prolog.cs.data.exchange.client.core.EtlTypeConstants;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.upcloud.base.inventory.model.WmsInvStockLot;
import com.prolog.upcloud.base.inventory.vo.InfTaskOutBackVO;
import io.swagger.annotations.Api;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

import static com.prolog.framework.common.message.RestMessage.doSuccess;

/**
 * @Description 质量管理
 * @Date 2021-05-08
 */

@Api(tags = "质量管理")
@RestController
@RequestMapping("/gspUpkeepCheck")
@Validated
public class GspUpkeepCheckController {

    @Autowired
    private GspUpkeepCheckService gspUpkeepCheckService;

    @Autowired
    private GspUpkeepCheckPlanService gspUpkeepCheckPlanService;

    @Autowired
    private ExchangeSender exchangeSender;

    @Autowired
    private AutoUpKeepTask autoUpKeepTask;

    @Autowired
    private GspUpkeepCheckService gspkeepCheckService;

    /**
     * 库存锁定，新增复检单
     */
    @ApiOperation("库存锁定，新增复检单")
    @PostMapping("/addStockLockCheck")
    public RestMessage<Object> addStockLockCheck(@RequestBody List<WmsInvStockLot> wmsInvStockLots) {
        gspkeepCheckService.batchAddStockLockDb(wmsInvStockLots);
        return doSuccess(null);
    }

    /**
     * 养护计划批量新增,养护记录批量新增
     */
    @ApiOperation("养护计划批量新增,养护记录批量新增")
    @PostMapping("/batchAdd")
    public RestMessage<Object> batchAdd(@Valid @NotEmpty(message = "参数不能为空") @RequestBody List<GspUpkeepCheck> list) {
        gspUpkeepCheckService.batchAdd("0", list);
        return doSuccess(null);
    }

    /**
     * 养护记录批量修改（保存养护记录）
     */
    @ApiOperation("养护记录批量修改")
    @PostMapping("/batchEdit")
    public RestMessage<Object> batchEdit(@RequestBody List<GspUpkeepCheck> gspUpkeepCheckList) {
        String[] arr = gspUpkeepCheckService.batchEdit(gspUpkeepCheckList, false);
        toOMS(arr);
        return RestMessage.newInstance(true, "success", null);
    }

    private void toOMS(String[] arr) {
        if (arr != null) {
            EtlMessage em = new EtlMessage();
            em.setId(String.join(",", arr));
            em.setEtlType(EtlTypeConstants.gsp_upkeep_w2o);
            try {
                exchangeSender.send(em);
            } catch (Exception e) {
                throw new BizException("同步OMS异常，请联系系统管理员");
            }
        }
    }

    /**
     * 养护记录批量审核
     */
    @ApiOperation("养护记录批量审核")
    @PostMapping("/batchCheck")
    public RestMessage<Object> batchCheck(@RequestBody List<GspUpkeepCheck> gspUpkeepCheckList) {
        String[] arr = gspUpkeepCheckService.batchEdit(gspUpkeepCheckList, true);
        return RestMessage.newInstance(true, "success", null);
    }

    /**
     * 分页查询养护列表（审核页面查询接口）
     */
    @ApiOperation("分页查询养护列表（审核页面查询接口）")
    @ApiImplicitParams({@ApiImplicitParam(name = "warehouseId", required = true, dataType = "String"), @ApiImplicitParam(name = "cargoOwnerId", required = true, dataType = "String"), @ApiImplicitParam(name = "status", required = false, dataType = "String"), @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "0"), @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "10")})
    @GetMapping("/listPage")
    public RestMessage<Page<GspUpkeepCheckVO>> listPage(@RequestParam String warehouseId, @RequestParam String cargoOwnerId, @RequestParam String status, @RequestParam(required = false) String billtype, @RequestParam Integer pageSize, @RequestParam Integer pageNum) throws Exception {
        return RestMessage.newInstance(true, "success", gspUpkeepCheckService.listPage(warehouseId, cargoOwnerId, status, billtype, pageSize, pageNum));
    }

    /**
     * 根据养护计划ID，查询
     */
    @Deprecated
    @ApiOperation("根据养护计划ID，查询")
    @GetMapping("/findByPlanId")
    public RestMessage<Page<GspUpkeepCheckVO>> findByPlanId(QueryDTO dto) {
        return RestMessage.newInstance(true, "success", gspUpkeepCheckService.findUpkeepPage(dto));
    }

    /**
     * 养护页面列表查询，查询
     */
    @ApiOperation("养护页面列表查询，查询")
    @GetMapping("/findUpkeepPage")
    public RestMessage<Page<GspUpkeepCheckVO>> findUpkeepPage(QueryDTO dto) {
        return RestMessage.newInstance(true, "success", gspUpkeepCheckService.findUpkeepPage(dto));
    }

    /**
     * 养护记录菜单，弹框接口：查询养护计划
     */
    @ApiOperation("养护记录菜单，弹框接口：查询养护计划")
    @GetMapping("/findPlan")
    public RestMessage<Page<GspUpkeepCheckPlan>> findPlan(@Valid QueryDTO dto) {
        return RestMessage.newInstance(true, "success", gspUpkeepCheckPlanService.findPlan(dto));
    }

    /**
     * 养护不合格品提取
     */
    @ApiOperation("养护不合格品提取")
    @GetMapping("/findUpkeep4MovePick")
    public RestMessage<List<GspUpkeepCheckPlan>> findUpkeep4MovePick(QueryDTO upkeepCheckDTO) {
        //		if(upkeepCheckDTO==null || StringUtils.isBlank(upkeepCheckDTO.getWarehouseId()) || StringUtils.isBlank(upkeepCheckDTO.getCargoOwnerId())){
        //			throw new ParameterException("入参错误");
        //		}
        List<GspUpkeepCheckPlan> upkeep4MovePick = gspUpkeepCheckPlanService.findUpkeepPlan4MovePick(upkeepCheckDTO);
        return RestMessage.newInstance(true, "success", upkeep4MovePick);
    }

    /**
     * 养护不合格品提取明细
     */
    @ApiOperation("养护不合格品提取明细")
    @GetMapping("/findUpkeepItem4MovePick")
    public RestMessage<List<GspUpkeepCheckVO>> findUpkeepItem4MovePick(String id) {
        return RestMessage.newInstance(true, "success", gspUpkeepCheckService.findUpkeepItem4MovePick(id));
    }

    @PostMapping("/confirmUpkeepMove")
    @ApiOperation(value = "养护移库确认")
    public RestMessage<Object> confirmUpkeepMove(@RequestBody String[] arrList) {
        gspUpkeepCheckService.confirmUpkeepMove(arrList);
        return RestMessage.newInstance(true, "success", null);
    }

    @GetMapping("/getLocationNoByLineId")
    @ApiOperation(value = "查询养护单的货格")
    public RestMessage<String> getLocationNoByLineId(@RequestParam String lineId) {
        return RestMessage.newInstance(true, "success", gspUpkeepCheckService.getLocationNoByLineId(lineId));
    }



    @GetMapping("/autoUpKeepTask")
    @ApiOperation(value = "定时任务生成养护计划")
    public RestMessage<Object> autoUpKeepTask(String warehouseId,String cargoOwnerId,String enterpriseId) {
        autoUpKeepTask.autoUpKeepTask(warehouseId,cargoOwnerId,enterpriseId);
        return RestMessage.newInstance(true, "success", null);
    }

    @UserLog("设备养护结果")
    @ApiOperation("设备养护结果")
    @PostMapping("/confirmDevice")
    public RestMessage<Object> confirmDevice() {
        gspUpkeepCheckService.confirmDevice();
        return RestMessage.newInstance(true, "success", null);
    }

    @UserLog("调用EIS出库任务推送")
    @ApiOperation("调用EIS出库任务推送-养护发送设备指令")
    @PostMapping("/sendTaskToEis")
    public RestMessage<Object> sendTaskToEis(@Valid @NotEmpty(message = "请勾选需要发送设备指令的养护记录信息") @RequestBody List<String> ids) {
        gspUpkeepCheckService.sendTaskToEis(ids);
        return doSuccess(null);
    }

    @ApiOperation(value = "出库任务回告")
    @PostMapping("/callbackPushTask")
    public RestMessage<Object> callbackPushTask(@RequestBody List<String> ids) {
        gspUpkeepCheckService.callbackPushTask(ids);
        return doSuccess(null);
    }

    @ApiOperation(value = "EIS出库任务回告-outbound服务调用，AGV货架和AGV托盘,四向库都走这个")
    @PostMapping("/gspUpKeepOutBack")
    public RestMessage<Object> gspUpKeepOutBack(@RequestBody List<InfTaskOutBackDTO> upkeepList) {
        gspUpkeepCheckService.gspUpKeepOutBack(upkeepList);
        return doSuccess(null);
    }

    /**
     * 库存解锁复检不合格转gsp移库
     */
    @ApiOperation("库存解锁复检不合格转gsp移库")
    @PostMapping("/addStockUnLock")
    public RestMessage<Object> addStockUnLock(@RequestBody List<WmsInvStockLot> wmsInvStockLots) {
        gspkeepCheckService.addStockUnLock(wmsInvStockLots);
        return doSuccess(null);
    }
}
