package com.bonus.bracelet.controller;

import com.bonus.bracelet.mapper.PersonMgeMapper;
import com.bonus.bracelet.service.IPersonMgeService;
import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.utils.DateTimeHelper;
import com.bonus.common.core.utils.UploadCheckUtils;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.entity.bracelet.BraceletParamsDto;
import com.bonus.common.entity.file.ResourceFileVo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.system.api.RemoteFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @className:PersonMgeController
 * @author:cwchen
 * @date:2024-07-16-11:03
 * @version:1.0
 * @description:人员管理-controller
 */
@RestController
@RequestMapping("/person/")
@Slf4j
public class PersonMgeController extends BaseController {

    @Resource(name = "IPersonMgeService")
    private IPersonMgeService service;

    @Resource
    private RemoteFileService remoteFileService;

    @Resource(name = "PersonMgeMapper")
    private PersonMgeMapper mapper;

    //    @RequiresPermissions("basic:person:list")
    @GetMapping("list")
    @SysLog(title = "人员管理", businessType = OperaType.QUERY, logType = 0, module = "基础管理->人员管理", details = "查询人员列表")
    public TableDataInfo list(BraceletParamsDto dto) {
        startPage();
        TableDataInfo tableDataInfo = service.getPersonLists(dto);
        return tableDataInfo;
    }

    //    @RequiresPermissions("basic:person:add")
    @PostMapping("addPerson")
    @SysLog(title = "人员管理", businessType = OperaType.INSERT, logType = 0, module = "基础管理->人员管理", details = "新增人员")
    public AjaxResult add(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file, String params) {
        return service.addPerson(file, params);
    }

    //    @RequiresPermissions("basic:person:edit")
    @PostMapping("editPerson")
    @SysLog(title = "人员管理", businessType = OperaType.INSERT, logType = 0, module = "基础管理->人员管理", details = "修改人员")
    public AjaxResult editPerson(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file, String params) {
        return service.editPerson(file, params);
    }

    @PostMapping("getPersonInfo")
    public AjaxResult getPersonInfo(@RequestBody BraceletParamsDto dto) {
        return service.getPersonInfo(dto);
    }

    //    @RequiresPermissions("basic:person:del")
    @PostMapping("delPerson")
    @SysLog(title = "人员管理", businessType = OperaType.DELETE, logType = 0, module = "基础管理->人员管理", details = "删除人员")
    public AjaxResult delPerson(@RequestBody BraceletParamsDto dto) {
        return service.delPerson(dto);
    }

    //    @RequiresPermissions("basic:person:push")
    @PostMapping("pushFace")
    @SysLog(title = "人员管理", businessType = OperaType.DELETE, logType = 0, module = "基础管理->人员管理", details = "推送人脸库")
    public AjaxResult pushFace(@RequestBody BraceletParamsDto dto) {
        return service.pushFace(dto);
    }

    @PostMapping("excelUpload")
//    @RequiresPermissions("basic:person:excelUpload")
    @SysLog(title = "人员管理", businessType = OperaType.IMPORT, logType = 0, module = "基础管理->人员管理", details = "人员导入")
    public AjaxResult excelUpload(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        String result = UploadCheckUtils.uploadExcelVerify(file);
        if (StringUtils.isNotBlank(result)) {
            return AjaxResult.error(result);
        }
        return service.excelUpload(file, request, response);
    }

    /**
     * 获取人脸照片
     * @param dto
     * @return AjaxResult
     * @author cwchen
     * @date 2024/8/21 9:27
     */
    @GetMapping("getPersonFace")
    public AjaxResult getPersonFace(BraceletParamsDto dto) {
        return service.getPersonFace(dto);
    }

    /**
     * 根据人员id获取证书详细信息
     */
    @RequiresPermissions("bracelet:person:query")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getCertificateInfo(@PathVariable Integer memberId) {
        return service.selectCertificateById(memberId);
    }

    /**
     * 获取文件地址
     * @param dto
     * @return
     */
    @PostMapping("getFileUrl")
    @SysLog(title = "人员管理", businessType = OperaType.DELETE, logType = 0, module = "基础管理->人员管理", details = "获取文件地址")
    public AjaxResult getFileUrl(@RequestBody ResourceFileVo dto) {
        return service.getFileUrl(dto);
    }

    /**
     * 修改证书
     */
    @RequiresPermissions("bracelet:person:edit")
    @PostMapping("updateCertificate")
    @SysLog(title = "人员管理", businessType = OperaType.UPDATE, module = "基础管理->人员管理")
    public AjaxResult editCertificate(@RequestParam(value = "highfile", required = false) MultipartFile[] highfile, @RequestParam(value = "electfile", required = false) MultipartFile[] electfile,
                                      @RequestParam(value = "elsefile", required = false) MultipartFile[] elsefile, Long id, @RequestParam(value = "delHighFileIdList", required = false) String[] delhighfile,
                                      @RequestParam(value = "delElectFileIdList", required = false) String[] delelectfile, @RequestParam(value = "delElseFileIdList", required = false) String[] delelsefile) {
        try {
            if (delhighfile != null) {
                for (int i = 0; i < delhighfile.length; i++) {
                    String high = String.valueOf(delhighfile[i]);
                    Integer certicificateId = mapper.getHighId(high);
//                    if(certicificateId==null){
//                        return AjaxResult.error("被删除的高出作业证图片资源无法找到");
//                    }
                    if (certicificateId != null) {
                        mapper.delHigh(certicificateId);
                        mapper.delHighFile(high, certicificateId);
                        remoteFileService.delFile(high, SecurityConstants.INNER);
                    }
                }
            }
            if (delelectfile != null) {
                for (int i = 0; i < delelectfile.length; i++) {
                    String elect = String.valueOf(delelectfile[i]);
                    Integer certicificateId = mapper.getElectId(elect);
//                    if(certicificateId==null){
//                        return AjaxResult.error("被删除的电工作业证作业证图片资源无法找到");
//                    }
                    if (certicificateId != null) {
                        mapper.delElect(certicificateId);
                        mapper.delElectFile(elect, certicificateId);
                        remoteFileService.delFile(elect, SecurityConstants.INNER);
                    }
                }
            }
            if (delelsefile != null) {
                for (int i = 0; i < delelsefile.length; i++) {
                    String elseFile = String.valueOf(delelsefile[i]);
                    Integer certicificateId = mapper.getElseId(elseFile);
//                    if(certicificateId==null){
//                        return AjaxResult.error("被删除的其他作业证作业证图片资源无法找到");
//                    }
                    if (certicificateId != null) {
                        mapper.delElse(certicificateId);
                        mapper.delElseFile(elseFile, certicificateId);
                        remoteFileService.delFile(elseFile, SecurityConstants.INNER);
                    }
                }
            }
            return service.updateCertificate(highfile, electfile, elsefile, id);

        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return error("系统异常");
    }
}
