package com.bonus.bracelet.controller;

import com.alibaba.fastjson2.JSON;
import com.bonus.bracelet.mapper.PersonMgeMapper;
import com.bonus.bracelet.service.IPersonMgeService;
import com.bonus.common.core.constant.BusinessConstants;
import com.bonus.common.core.constant.SecurityConstants;
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.bracelet.vo.BaseProject;
import com.bonus.common.entity.bracelet.vo.PersonVo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.common.security.utils.SecurityUtils;
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();
        List<PersonVo> list = service.getPersonLists(dto);
        return getDataTable(list);
    }

//    @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);
    }

    @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);
    }

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

    /**
     * 修改证书
     */
    @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]);
                    int certicificateId = mapper.getHighId(high);
                    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]);
                    int certicificateId =   mapper.getElectId(elect);
                    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]);
                    int certicificateId =   mapper.getElseId(elseFile);
                    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("系统异常");
    }
}
