package com.yanzhu.jh.project.controller;

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

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.SysRoleEnum;
import com.ruoyi.common.utils.file.ImageUtils;
import com.ruoyi.system.service.ISysDeptService;
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.SurProjectWorkAttendance;
import com.yanzhu.jh.project.service.ISurProjectWorkAttendanceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目出勤Controller
 * 
 * @author JiangYuQi
 * @date 2023-08-09
 */
@RestController
@RequestMapping("/project/surProjectAttendance")
public class SurProjectWorkAttendanceController extends BaseController
{
    @Autowired
    private ISurProjectWorkAttendanceService surProjectWorkAttendanceService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询项目出勤列表
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectAttendance:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurProjectWorkAttendance surProjectWorkAttendance)
    {
        startPage();
        surProjectWorkAttendance.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(surProjectWorkAttendance.getNowRole())){
            surProjectWorkAttendance.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            surProjectWorkAttendance.setNowDept(Convert.toStr(getDeptId()));
        }
        surProjectWorkAttendance.setNowUser(Convert.toStr(getUserId()));
        List<SurProjectWorkAttendance> list = surProjectWorkAttendanceService.selectSurProjectWorkAttendanceList(surProjectWorkAttendance);
        return getDataTable(list);
    }

    /**
     * 查询项目出勤列表
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectAttendance:list')")
    @GetMapping("/viewData")
    public TableDataInfo viewData(SurProjectWorkAttendance surProjectWorkAttendance)
    {
        if(surProjectWorkAttendance.getWorkDate()==null){
            surProjectWorkAttendance.setWorkDate(new Date());
        }
        List<SurProjectWorkAttendance> list = surProjectWorkAttendanceService.selectSurProjectWorkAttendanceList(surProjectWorkAttendance);
        return getDataTable(list);
    }

    /**
     * 导出项目出勤列表
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectAttendance:export')")
    @Log(title = "项目出勤", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurProjectWorkAttendance surProjectWorkAttendance)
    {
        surProjectWorkAttendance.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(surProjectWorkAttendance.getNowRole())){
            surProjectWorkAttendance.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            surProjectWorkAttendance.setNowDept(Convert.toStr(getDeptId()));
        }
        surProjectWorkAttendance.setNowUser(Convert.toStr(getUserId()));
        List<SurProjectWorkAttendance> list = surProjectWorkAttendanceService.selectSurProjectWorkAttendanceList(surProjectWorkAttendance);
        ExcelUtil<SurProjectWorkAttendance> util = new ExcelUtil<SurProjectWorkAttendance>(SurProjectWorkAttendance.class);
        util.exportExcel(response, list, "项目出勤数据");
    }

    /**
     * 获取项目出勤详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectAttendance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surProjectWorkAttendanceService.selectSurProjectWorkAttendanceById(id));
    }

    /**
     * 新增项目出勤
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectAttendance:add')")
    @Log(title = "项目出勤", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurProjectWorkAttendance surProjectWorkAttendance)
    {
        return toAjax(surProjectWorkAttendanceService.insertSurProjectWorkAttendance(surProjectWorkAttendance));
    }

    /**
     * 修改项目出勤
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectAttendance:edit')")
    @Log(title = "项目出勤", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurProjectWorkAttendance surProjectWorkAttendance)
    {
        return toAjax(surProjectWorkAttendanceService.updateSurProjectWorkAttendance(surProjectWorkAttendance));
    }

    /**
     * 删除项目出勤
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectAttendance:remove')")
    @Log(title = "项目出勤", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surProjectWorkAttendanceService.deleteSurProjectWorkAttendanceByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('project:surProjectAttendance:query')")
    @PostMapping("/selectByDate")
    public AjaxResult selectByDate(@RequestBody SurProjectWorkAttendance surProjectWorkAttendance){
        return AjaxResult.success(surProjectWorkAttendanceService.selectByDate(surProjectWorkAttendance));
    }
}
