package com.skr.mdm.api.cmd.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.skr.mdm.api.cmd.service.CmdManageService;
import com.skr.mdm.bean.qo.cmd.CmdPageQO;
import com.skr.mdm.bean.qo.cmd.CmdPushQO;
import com.skr.mdm.bean.vo.equipment.SendCmdMessageInfoVO;
import com.skr.mdm.dao.AuAdminDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.entity.AuAdministratorDO;
import com.skr.mdm.enums.CmdAdminEnum;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author nfq
 * @date 2023-03-21 9:45
 */
@Validated
@RestController
@RequestMapping("cmd")
@Api(tags = "指令管理")
public class CmdController {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private CmdManageService cmdManageService;
    @Autowired
    private AuAdminDao adminDao;

    @PostMapping("push")
    @ApiOperation("指令推送")
    @PreAuthorize("hasAnyAuthority(new String[] {'sys:equipment:security:lock', 'sys:equipment:security:unlock'})")
    public ResponseResult push(@Valid @RequestBody CmdPushQO cmdPushQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return cmdManageService.cmdHandle(cmdPushQO, loginAdminDTO);
    }

    @PostMapping("security/push")
    @ApiOperation("指令推送(需密码校验指令)")
    @PreAuthorize("hasAnyAuthority(new String[] {'sys:equipment:security:lost', 'sys:equipment:security:eliminate', 'sys:equipment:security:retrieve'})")
    public ResponseResult securityPush(@Valid @RequestBody CmdPushQO cmdPushQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        if (StringUtils.isBlank(cmdPushQO.getPassword())) {
            return ResponseResult.failure("密码不能为空");
        }
        AuAdministratorDO administratorDO = adminDao.selectById(loginAdminDTO.getId());
        String dbPassword = administratorDO.getPassword();
        if (!passwordEncoder.matches(cmdPushQO.getPassword(), dbPassword)) {
            return ResponseResult.failure("密码校验失败");
        }
        return cmdManageService.cmdHandle(cmdPushQO, loginAdminDTO);
    }

    @PostMapping("user/list")
    @ApiOperation(value = "指令作用人列表", response = SendCmdMessageInfoVO.class)
    @PreAuthorize("hasAnyAuthority(new String[] {'sys:equipment:security:lock', 'sys:equipment:security:unlock'," +
            "'sys:equipment:security:restore_factory'})")
    public ResponseResult listCmdUser(@Valid @RequestBody CmdPageQO cmdPageQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        if ("lossEq".equals(cmdPageQO.getCmd())) {
            cmdPageQO.setCmd(CmdAdminEnum.LOSS_EQ_CLEAR_DATA.getCmd());
        }
        return cmdManageService.listCmdUser(cmdPageQO, loginAdminDTO);
    }

}