package com.prolog.upcloud.base.wh.operate.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.prolog.framework.bz.common.search.SearchApi;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.framework.utils.JsonUtils;
import com.prolog.upcloud.base.wh.operate.service.InvCheckService;
import com.prolog.upcloud.base.wh.operate.vo.WmsInvCheckAddVO;
import com.prolog.upcloud.base.wh.operate.vo.WmsInvCheckDVO;
import com.prolog.upcloud.base.wh.operate.vo.WmsInvCheckUpdateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

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

/**
 * @author zzz
 * @desc
 * @date 2021/4/9
 */
@Api(tags = "盘点管理")
@RestController
@RequestMapping("invcheck")
@Validated
public class InvCheckController {

    private static Logger logger = LoggerFactory.getLogger(InvCheckController.class);

    @Autowired
    private SearchApi searchApi;

    @Autowired
    private InvCheckService invCheckService;

    @UserLog
    @ApiOperation(value = "新增盘点计划")
    @PostMapping("/add")
    public RestMessage<?> addInvCheck(@RequestBody WmsInvCheckAddVO invCheckAddVO) {
        invCheckService.addInvCheck(invCheckAddVO);
        return success("操作成功", null);
    }

    @UserLog
    @ApiOperation(value = "新增全部盘点计划")
    @PostMapping("addInvCheckAll")
    public RestMessage<?> addInvCheckAll(@RequestBody Map map) {
        try {
            List<Map> list = searchApi.searchForMap("578638984813613056", map);
            WmsInvCheckAddVO invCheckAddVO = new WmsInvCheckAddVO();
            invCheckAddVO.setCheckType(map.get("checkType").toString());
            invCheckAddVO.setCheckMethod(map.get("checkWay").toString());
            List<WmsInvCheckDVO> dList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                WmsInvCheckDVO detail = JsonUtils.parseJsonString(JsonUtils.toString(list.get(i)), new TypeReference<WmsInvCheckDVO>() {
                });
                Object warehouseLocatorId = list.get(i).get("warehouseLocatorId");
                if (warehouseLocatorId != null) {
                    detail.setWhLocatorId(warehouseLocatorId.toString());
                }
                dList.add(detail);
            }
            invCheckAddVO.setInvCheckDVOList(dList);
            invCheckService.addInvCheck(invCheckAddVO);
        } catch (Exception e) {
            logger.info("系统异常", e);
            throw new BizException("系统异常");
        }
        return success("操作成功", null);
    }

    /**
     * 盘点录入(有计划)
     *
     * @param list
     * @return
     */
    @UserLog
    @ApiOperation(value = "盘点录入保存，生成差异")
    @PostMapping("/update")
    public RestMessage<?> updateInvCheck(@Valid @NotEmpty(message = "请勾选需要操作的数据") @RequestBody List<WmsInvCheckUpdateVO> list) {
        invCheckService.updateInvCheck(list);
        return success("操作成功", null);
    }

    /**
     * 盘点差异审核(有计划)
     *
     * @param map
     * @return
     */
    @UserLog
    @ApiOperation(value = "盘点差异审核")
    @ApiImplicitParams({@ApiImplicitParam(name = "map", value = "{\"type\":\"审核类型（15：审核通过转损溢 20：作废）\",\"checkLineIds\":\"多id逗号分隔\"}")})
    @PostMapping("examine")
    public RestMessage<?> examineInvCheck(@RequestBody Map<String, Object> map) {
        invCheckService.examineInvCheck(map);
        return success("操作成功", null);
    }

    /**
     * 复盘(有计划)
     */
    @UserLog
    @ApiOperation(value = "生成复盘")
    @ApiImplicitParams({@ApiImplicitParam(name = "map", value = "{\"checkLineIds\":\"多id逗号分隔\"}")})
    @PostMapping("add-re")
    public RestMessage<?> addInvCheckRe(@RequestBody Map<String, Object> map) {
        invCheckService.addInvCheckRe(map);
        return success("操作成功", null);
    }

    /**
     * 明盘打印
     */

    /**
     * 暗盘打印
     */


    @ApiOperation(value = "盘点录入发送设备指令")
    @PostMapping("/sendDeviceOrder4Check")
    public RestMessage<Object> sendDeviceOrder4Check(@Valid @NotEmpty(message = "请勾选需要发送设备指令的盘点记录信息") @RequestBody List<String> list) {
        invCheckService.sendDeviceOrder4Check(list);
        return doSuccess(null);
    }
}
