package com.yanzhu.jh.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.yanzhu.jh.project.domain.SurProjectAttendanceCfg;
import com.yanzhu.jh.project.service.ISurProjectAttendanceCfgService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目考勤配置Controller
 * 
 * @author ruoyi
 * @date 2023-09-24
 */
@RestController
@RequestMapping("/project/attendanceConfig")
public class SurProjectAttendanceCfgController extends BaseController
{
    @Autowired
    private ISurProjectAttendanceCfgService surProjectAttendanceCfgService;

    /**
     * 查询项目考勤配置列表
     */
    @PreAuthorize("@ss.hasPermi('project:attendanceConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurProjectAttendanceCfg surProjectAttendanceCfg)
    {
        startPage();
        List<SurProjectAttendanceCfg> list = surProjectAttendanceCfgService.selectSurProjectAttendanceCfgList(surProjectAttendanceCfg);
        return getDataTable(list);
    }

    /**
     * 导出项目考勤配置列表
     */
    @PreAuthorize("@ss.hasPermi('project:attendanceConfig:export')")
    @Log(title = "项目考勤配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurProjectAttendanceCfg surProjectAttendanceCfg)
    {
        List<SurProjectAttendanceCfg> list = surProjectAttendanceCfgService.selectSurProjectAttendanceCfgList(surProjectAttendanceCfg);
        ExcelUtil<SurProjectAttendanceCfg> util = new ExcelUtil<SurProjectAttendanceCfg>(SurProjectAttendanceCfg.class);
        util.exportExcel(response, list, "项目考勤配置数据");
    }

    /**
     * 获取项目考勤配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:attendanceConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surProjectAttendanceCfgService.selectSurProjectAttendanceCfgById(id));
    }

    /**
     * 新增项目考勤配置
     */
    @PreAuthorize("@ss.hasPermi('project:attendanceConfig:add')")
    @Log(title = "项目考勤配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurProjectAttendanceCfg surProjectAttendanceCfg)
    {
        SurProjectAttendanceCfg where=new SurProjectAttendanceCfg();
        where.setProjectId(surProjectAttendanceCfg.getProjectId());
        where.setSubDeptId(surProjectAttendanceCfg.getSubDeptId());
        where.setVendorsCode(surProjectAttendanceCfg.getVendorsCode());
        List<SurProjectAttendanceCfg> list=surProjectAttendanceCfgService.selectSurProjectAttendanceCfgList(where);
        if(list.size()>0){
            SurProjectAttendanceCfg it=list.get(0);
            return AjaxResult.error("项目【"+it.getProjectName()+"】总包单位【"+it.getUnitName()+"】已添加过此厂商【"+it.getVendorName()+"】的配置了！");
        }
        return toAjax(surProjectAttendanceCfgService.insertSurProjectAttendanceCfg(surProjectAttendanceCfg));
    }

    /**
     * 修改项目考勤配置
     */
    @PreAuthorize("@ss.hasPermi('project:attendanceConfig:edit')")
    @Log(title = "项目考勤配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurProjectAttendanceCfg surProjectAttendanceCfg)
    {
        return toAjax(surProjectAttendanceCfgService.updateSurProjectAttendanceCfg(surProjectAttendanceCfg));
    }

    /**
     * 删除项目考勤配置
     */
    @PreAuthorize("@ss.hasPermi('project:attendanceConfig:remove')")
    @Log(title = "项目考勤配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surProjectAttendanceCfgService.deleteSurProjectAttendanceCfgByIds(ids));
    }
}
