package com.skr.mdm.api.cmd.service.impl;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.skr.mdm.api.cmd.CmdAdminHandlerFactory;
import com.skr.mdm.api.cmd.service.CmdManageService;
import com.skr.mdm.bean.bo.CmdInfoBO;
import com.skr.mdm.bean.qo.cmd.CmdPageQO;
import com.skr.mdm.bean.qo.cmd.CmdPushQO;
import com.skr.mdm.bean.qo.cmd.CmdQO;
import com.skr.mdm.bean.so.CmdInfoSO;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.enums.CmdAdminEnum;
import com.skr.mdm.enums.LogAdminTypeEnum;
import com.skr.mdm.enums.ResultCodeEnum;
import com.skr.mdm.handle.IssuedUserHandler;
import com.skr.mdm.handle.LogAdminHandler;
import com.skr.mdm.result.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;

/**
 * @author nfq
 * @date 2021/10/28 15:49
 */
@Service
@Slf4j
public class CmdManageServiceImpl implements CmdManageService {

    private final String MODULE = "指令管理";

    @Autowired
    private LogAdminHandler logAdminHandler;
    @Autowired
    private IssuedUserHandler issuedUserHandler;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseResult cmdHandle(CmdPushQO cmdPushQO, LoginAdminDTO loginAdminDTO) {
        // save db
        CmdInfoBO cmdInfoBO = CmdInfoBO.builder()
                .equipmentIdList(cmdPushQO.getEquipmentIdList())
                .cmdType(cmdPushQO.getCmd())
                .msg(cmdPushQO.getCmdInfo())
                .build();
        Long msgId = CmdAdminHandlerFactory.getStrategy(cmdPushQO.getCmd()).saveCmd(cmdInfoBO, loginAdminDTO);
        // issued
        CmdInfoSO cmdInfo = new CmdInfoSO();
        cmdInfo.setActionId(msgId);
        cmdInfo.setMsg(cmdPushQO.getCmdInfo());
        cmdInfo.setSendTime(new Date());
        issuedUserHandler.issuedUser(cmdPushQO.getEquipmentIdList(), loginAdminDTO.getCompanyId(), cmdPushQO.getCmd(),
                JSONUtil.toJsonStr(Collections.singletonList(cmdInfo)));
        // log
        logAdminHandler.log(loginAdminDTO, LogAdminTypeEnum.INSTRUCT_TYPE.getLogType(),
                CmdAdminEnum.getValue(cmdPushQO.getCmd()).getCmdName(), MODULE, cmdPushQO.getEquipmentIdList());
        return ResponseResult.success();
    }

    @Override
    public ResponseResult listCmdUser(CmdPageQO cmdPageQO, LoginAdminDTO loginAdminDTO) {
        CmdAdminEnum cmdAdminEnum = CmdAdminEnum.getValue(cmdPageQO.getCmd());
        if (cmdAdminEnum == null) {
            return ResponseResult.failure(ResultCodeEnum.INSTRUCT_NOT_EXIST);
        }
        Integer cmdValue = cmdAdminEnum.getCmdValue();
        CmdQO cmdQO = CmdQO.builder()
                .companyId(loginAdminDTO.getCompanyId())
                .searchType(cmdPageQO.getSearchType())
                .searchValue(cmdPageQO.getSearchValue())
                .cmdType(cmdValue)
                .build();
        PageHelper.startPage(cmdPageQO.getPageNum(), cmdPageQO.getPageSize());
        return CmdAdminHandlerFactory.getStrategy(cmdPageQO.getCmd()).cmdUserInfo(cmdQO);
    }


}