package com.jeethink.receive.controller;

import com.jeethink.bean.domain.ArchivesMaster;
import com.jeethink.common.annotation.Log;
import com.jeethink.common.constant.Constants;
import com.jeethink.common.core.controller.BaseController;
import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.core.domain.entity.SysUser;
import com.jeethink.common.core.domain.model.LoginUser;
import com.jeethink.common.core.page.TableDataInfo;
import com.jeethink.common.enums.BusinessType;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.common.utils.SecurityUtils;
import com.jeethink.common.utils.ServletUtils;
import com.jeethink.common.utils.StringUtils;
import com.jeethink.common.utils.file.FileDownLoad;
import com.jeethink.common.utils.poi.ExcelUtil;
import com.jeethink.common.utils.uuid.IdUtils;
import com.jeethink.receive.service.IArchivesService;
import com.jeethink.receive.vo.ArchivesVO;
import com.jeethink.system.domain.SystemFile;
import com.jeethink.system.service.ISystemFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * 人事档案接收Controller
 *
 * @author sh
 * @date 2022-11-08
 */
@Api("人事档案接收")
@RestController
@RequestMapping("/receive/rsReceive")
public class RsArchivesController extends BaseController {

    @Autowired
    private IArchivesService service;
    @Autowired
    private ISystemFileService systemFileService;

    /**
     * 查询人事档案接收列表
     */
    @ApiOperation("人事档案接收页面list列表")
    @PreAuthorize("@ss.hasPermi('receive:rsReceive:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArchivesMaster archives) {

        startPage();
        archives.setType(Constants.ARCHIVES_TYPE_RS);
        archives.setStage(Constants.ARCHIVES_STAGE_RECEIVE);
        List<ArchivesMaster> list = service.selectArchivesList(archives);
        return getDataTable(list);
    }

    /**
     * 获取人事档案接收详细信息
     */
    @PreAuthorize("@ss.hasPermi('receive:rsReceive:query')")
    @GetMapping
    public AjaxResult getInfo(@RequestParam String archivesId) {

        return AjaxResult.success(service.selectArchivesById(archivesId));
    }

    @PreAuthorize("@ss.hasPermi('receive:rsReceive:add')")
    @Log(title = "人事档案接收", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArchivesVO archives) {

        archives.setType(Constants.ARCHIVES_TYPE_RS);
        service.insertArchives(archives);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('receive:rsReceive:edit')")
    @Log(title = "人事档案接收", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody ArchivesVO archives) {

        service.updateArchives(archives);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('receive:rsReceive:submit')")
    @Log(title = "人事档案接收", businessType = BusinessType.OTHER)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody ArchivesVO archives) {

        service.submitArchives(archives);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('receive:rsReceive:revocation')")
    @Log(title = "人事档案接收", businessType = BusinessType.OTHER)
    @GetMapping("/revocation")
    public AjaxResult revocation(@RequestParam String archivesId) {

        service.revocationArchives(archivesId);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('receive:rsReceive:remove')")
    @Log(title = "人事档案接收", businessType = BusinessType.DELETE)
    @DeleteMapping("/{archivesId}")
    public AjaxResult remove(@PathVariable("archivesId") String archivesId) {

        return toAjax(service.removeArchives(archivesId));
    }

    /**
     * 导出人事档案接收列表
     */
    @PreAuthorize("@ss.hasPermi('receive:rsReceive:export')")
    @Log(title = "人事档案接收", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody ArchivesMaster archives) {
        archives.setType(Constants.ARCHIVES_TYPE_RS);
        archives.setStage(Constants.ARCHIVES_STAGE_RECEIVE);
        List<ArchivesMaster> list = service.selectArchivesList(archives);
        ExcelUtil<ArchivesMaster> util = new ExcelUtil<ArchivesMaster>(ArchivesMaster.class);
        return util.exportExcel(list, "rsReceive");
    }

    @Log(title = "档案导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('receive:rsReceive:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ArchivesMaster> util = new ExcelUtil<ArchivesMaster>(ArchivesMaster.class);
        List<ArchivesMaster> archivesMasterList = util.importExcel(file.getInputStream());
        String message = service.importArchives(archivesMasterList, updateSupport);
        return AjaxResult.success(message);
    }
    /**
     * 批量导入附件并与主表关联
     * @return
     */
    @GetMapping("/uploadArchiveList")
    public AjaxResult uploadArchiveList(){
        service.uploadArchiveFileList();
        return AjaxResult.success();
    }
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<ArchivesMaster> util = new ExcelUtil<ArchivesMaster>(ArchivesMaster.class);
        return util.importTemplateExcel("档案模板");
    }

    /**
     * 与朗新接口对接获取年度工资表数据（URL、人员编号、文件类型、文件大小）
     * @param salaryList
     */
    @ApiOperation("年度工资单")
    @PostMapping("/salary")
    @CrossOrigin(origins = "*")
    public AjaxResult rsSalary(@RequestBody List<HashMap> salaryList){
        for(int i=0;i<salaryList.size();i++){
        String fileUrl= (String) salaryList.get(i).get("fileUrl");
        String userNumber= (String) salaryList.get(i).get("userNumber");
        String type= (String) salaryList.get(i).get("type");
        int fileSize= (int) salaryList.get(i).get("fileSize");
        String fileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1);
        String filePath = "E:\\jeethink\\uploadPath\\uploadFiles\\archives\\person\\"+userNumber+"\\"+type;
        File file = FileDownLoad.saveUrlAs(fileUrl, filePath,fileName ,"GET");
        String fileObjectId = service.selectArchiveFileId(userNumber,type);
            if(StringUtils.isNotNull(fileObjectId)){
                SystemFile systemFile = new SystemFile();
                systemFile.setFileId(IdUtils.simpleUUID());
                systemFile.setFileObjectId(fileObjectId);
                systemFile.setFileSize((long) fileSize);
                systemFile.setFileName(fileName.split("\\.")[0]);
                systemFile.setFileSuffix(fileName.split("\\.")[1]);
                systemFile.setFilePath(filePath);
                String fileUrl2 = "http://10.10.10.141:9001/profile/uploadFiles/archives/person/"+userNumber+"/"+type+"/"+fileName;
                systemFile.setFileUrl(fileUrl2);
                systemFile.setFileDeleteType(Constants.DELETED_NO);
                systemFile.setFileCreateTime(DateUtils.getNowDate());
                systemFileService.insertSystemFile(systemFile);
            }
        }
        return AjaxResult.success();
    }

}
