package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.ApplyCfgTypeEnum;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.ShiFouEnum;
import com.ruoyi.common.utils.NoUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysApplyConfig;
import com.ruoyi.system.service.ISysApplyConfigService;
import com.yanzhu.jh.project.domain.SurProjectAttendanceCfg;
import com.yanzhu.jh.project.service.ISurProjectAttendanceCfgService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统应用注册Controller
 * 
 * @author JiangYuQi
 * @date 2024-01-13
 */
@RestController
@RequestMapping("/system/applyConfig")
public class SysApplyConfigController extends BaseController
{
    private static final String vendorsCode = "yanzhu";

    @Autowired
    private ISysApplyConfigService sysApplyConfigService;

    @Autowired
    private ISurProjectAttendanceCfgService surProjectAttendanceCfgService;

    /**
     * 查询系统应用注册列表
     */
    @PreAuthorize("@ss.hasPermi('system:applyConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysApplyConfig sysApplyConfig)
    {
        startPage();
        List<SysApplyConfig> list = sysApplyConfigService.selectSysApplyConfigList(sysApplyConfig);
        return getDataTable(list);
    }

    /**
     * 导出系统应用注册列表
     */
    @PreAuthorize("@ss.hasPermi('system:applyConfig:export')")
    @Log(title = "系统应用注册", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysApplyConfig sysApplyConfig)
    {
        List<SysApplyConfig> list = sysApplyConfigService.selectSysApplyConfigList(sysApplyConfig);
        ExcelUtil<SysApplyConfig> util = new ExcelUtil<SysApplyConfig>(SysApplyConfig.class);
        util.exportExcel(response, list, "系统应用注册数据");
    }

    /**
     * 获取系统应用注册详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:applyConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysApplyConfigService.selectSysApplyConfigById(id));
    }

    /**
     * 新增系统应用注册
     */
    @PreAuthorize("@ss.hasPermi('system:applyConfig:add')")
    @Log(title = "系统应用注册", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysApplyConfig sysApplyConfig)
    {
        if(ApplyCfgTypeEnum.LABOUR.getCode().equals(sysApplyConfig.getCfgType())){
            SurProjectAttendanceCfg surProjectAttendanceCfg = new SurProjectAttendanceCfg();
            surProjectAttendanceCfg.setProjectId(sysApplyConfig.getProjectId());
            surProjectAttendanceCfg.setSubDeptId(sysApplyConfig.getDeptId());
            surProjectAttendanceCfg.setVendorsCode(vendorsCode);
            surProjectAttendanceCfg.setEnabled(sysApplyConfig.getIsDel().equals(ShiFouEnum.FOU.getCode())?ShiFouEnum.SHI.getLongCode():ShiFouEnum.FOU.getLongCode());
            surProjectAttendanceCfg.setState(ShiFouEnum.FOU.getLongCode());
            int res = surProjectAttendanceCfgService.insertSurProjectAttendanceCfg(surProjectAttendanceCfg);
            if(res>0){
                // 同步将信息保存到sur_project_attendance_cfg
                sysApplyConfig.setCfgId(surProjectAttendanceCfg.getId());
            }
        }
        return toAjax(sysApplyConfigService.insertSysApplyConfig(sysApplyConfig));
    }

    /**
     * 修改系统应用注册
     */
    @PreAuthorize("@ss.hasPermi('system:applyConfig:edit')")
    @Log(title = "系统应用注册", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysApplyConfig sysApplyConfig)
    {
        if(ApplyCfgTypeEnum.LABOUR.getCode().equals(sysApplyConfig.getCfgType())){
            SurProjectAttendanceCfg surProjectAttendanceCfg = new SurProjectAttendanceCfg();
            surProjectAttendanceCfg.setId(sysApplyConfig.getCfgId());
            surProjectAttendanceCfg.setEnabled(sysApplyConfig.getIsDel().equals(ShiFouEnum.FOU.getCode())?ShiFouEnum.SHI.getLongCode():ShiFouEnum.FOU.getLongCode());
            surProjectAttendanceCfgService.updateSurProjectAttendanceCfg(surProjectAttendanceCfg);
        }
        return toAjax(sysApplyConfigService.updateSysApplyConfig(sysApplyConfig));
    }

    /**
     * 删除系统应用注册
     */
    @PreAuthorize("@ss.hasPermi('system:applyConfig:remove')")
    @Log(title = "系统应用注册", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> cfgIds = new ArrayList<>();
        for(Long id:ids){
            SysApplyConfig sysApplyConfig = sysApplyConfigService.selectSysApplyConfigById(id);
            if(ApplyCfgTypeEnum.LABOUR.getCode().equals(sysApplyConfig.getCfgType())){
                cfgIds.add(sysApplyConfig.getCfgId());
            }
        }
        if(CollectionUtils.isNotEmpty(cfgIds)){
            surProjectAttendanceCfgService.deleteSurProjectAttendanceCfgByIds(cfgIds.stream().toArray(Long[]::new));
        }
        return toAjax(sysApplyConfigService.deleteSysApplyConfigByIds(ids));
    }

    /**
     * 生成AppId
     */
    @PreAuthorize("@ss.hasPermi('system:applyConfig:add')")
    @GetMapping("/createAppId")
    public AjaxResult createAppId()
    {
        return success(NoUtils.createAppId());
    }

}
