package com.willalp.web.controller.clockingin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.files.domain.HsOrgPersonnelFiles;
import com.willalp.files.service.IHsOrgPersonnelFilesService;
import com.willalp.system.domain.SysUserOrganization;
import com.willalp.system.service.ISysUserOrganizationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 机构人员档案Controller
 *
 * @author willalp
 * @date 2022-08-01
 */
@RestController
@RequestMapping("/files/files")
public class HsOrgPersonnelFilesController extends BaseController {
    @Resource
    private IHsOrgPersonnelFilesService hsOrgPersonnelFilesService;

    @Resource
    private ISysUserOrganizationService suoService;

    /**
     * 查询机构人员档案列表
     */
    @PreAuthorize("@ss.hasPermi('files:files:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsOrgPersonnelFiles hsOrgPersonnelFiles) {
        startPage();
        List<HsOrgPersonnelFiles> list = hsOrgPersonnelFilesService.selectHsOrgPersonnelFilesList(hsOrgPersonnelFiles);
        return getDataTable(list);
    }

    /**
     * 导出机构人员档案列表
     */
    @PreAuthorize("@ss.hasPermi('files:files:export')")
    @Log(title = "机构人员档案", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsOrgPersonnelFiles hsOrgPersonnelFiles) {
        List<HsOrgPersonnelFiles> list = hsOrgPersonnelFilesService.importHsOrgPersonnelFilesList(hsOrgPersonnelFiles);
        ExcelUtil<HsOrgPersonnelFiles> util = new ExcelUtil<HsOrgPersonnelFiles>(HsOrgPersonnelFiles.class);
        return util.exportExcel(list, "【人员档案数据】");
    }

    @Log(title = "工资条基础", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<HsOrgPersonnelFiles> util = new ExcelUtil<>(HsOrgPersonnelFiles.class);
        //获取数据库对应数据
        List<HsOrgPersonnelFiles> list = util.importExcel(file.getInputStream());
        return AjaxResult.success(hsOrgPersonnelFilesService.importData(list));
    }

    @GetMapping("/importTemp")
    public AjaxResult importTemp() {
        ExcelUtil<HsOrgPersonnelFiles> util = new ExcelUtil<>(HsOrgPersonnelFiles.class);
        return util.importTemplateExcel("【人员档案数据导入模板】");
    }

    /**
     * 获取机构人员档案详细信息`
     */
    @PreAuthorize("@ss.hasPermi('files:files:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(hsOrgPersonnelFilesService.getById(id));
    }

    /**
     * 新增机构人员档案
     */
    @PreAuthorize("@ss.hasPermi('files:files:add')")
    @Log(title = "机构人员档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsOrgPersonnelFiles hsOrgPersonnelFiles) {
        hsOrgPersonnelFiles.setCreateBy(SecurityUtils.getUsername());
        hsOrgPersonnelFiles.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsOrgPersonnelFilesService.save(hsOrgPersonnelFiles));
    }

    /**
     * 修改机构人员档案
     */
    @PreAuthorize("@ss.hasPermi('files:files:edit')")
    @Log(title = "机构人员档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsOrgPersonnelFiles hsOrgPersonnelFiles) {
        SysUserOrganization sysUserOrganization = null;
        if (StringUtils.isNotBlank(hsOrgPersonnelFiles.getUserCode()) &&
                StringUtils.isNotBlank(hsOrgPersonnelFiles.getOrgCode())) {
            sysUserOrganization = suoService.getBaseMapper().selectOne(new QueryWrapper<SysUserOrganization>()
                    .eq("login_name", hsOrgPersonnelFiles.getUserCode())
                    .eq("organization_code", hsOrgPersonnelFiles.getOrgCode()));
        }
        if (null != sysUserOrganization) {
            String userType = hsOrgPersonnelFiles.getUserType();
            if (StringUtils.isNotBlank(userType)) {
                sysUserOrganization.setUserType(hsOrgPersonnelFiles.getUserType());
            }
        } else {
            return AjaxResult.error("请先绑定机构");
        }
        suoService.updateById(sysUserOrganization);
        hsOrgPersonnelFiles.setUpdateBy(SecurityUtils.getUsername());
        hsOrgPersonnelFiles.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsOrgPersonnelFilesService.updateById(hsOrgPersonnelFiles));
    }

    /**
     * 删除机构人员档案
     */
    @PreAuthorize("@ss.hasPermi('files:files:remove')")
    @Log(title = "机构人员档案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsOrgPersonnelFilesService.removeByIds(Arrays.asList(ids)));
    }
}
