package com.ruoyi.web.controller.oa;

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

import com.ruoyi.oa.domain.CmcPerformanceStaff;
import com.ruoyi.oa.domain.CmcProject;
import com.ruoyi.oa.service.ICmcProjectService;
import com.ruoyi.system.service.ISysUserService;
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.ruoyi.oa.service.ICmcPerformanceStaffService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * cmc员工绩效Controller
 * 
 * @author cmc
 * @date 2025-06-04
 */
@RestController
@RequestMapping("/oa/performanceStaff")
public class CmcPerformanceStaffController extends BaseController
{
    @Autowired
    private ICmcPerformanceStaffService cmcPerformanceStaffService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ICmcProjectService projectService;

    /**
     * 查询cmc员工绩效列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcPerformanceStaff cmcPerformanceStaff)
    {
        startPage();
        List<CmcPerformanceStaff> list = cmcPerformanceStaffService.selectCmcPerformanceStaffList(cmcPerformanceStaff);
        return getDataTable(list);
    }

    /**
     * 获取cmc员工绩效详细信息
     */
    @GetMapping(value = "/monthPerformance")
    public AjaxResult getMonthPerformanceList(String performanceId, String deptId)
    {
        return success(cmcPerformanceStaffService.selectMonthPerformanceList(performanceId, deptId));
    }

    /**
     * 导出cmc员工绩效列表
     */
    @Log(title = "cmc员工绩效", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcPerformanceStaff cmcPerformanceStaff)
    {
        List<CmcPerformanceStaff> list = cmcPerformanceStaffService.selectCmcPerformanceStaffList(cmcPerformanceStaff);
        ExcelUtil<CmcPerformanceStaff> util = new ExcelUtil<CmcPerformanceStaff>(CmcPerformanceStaff.class);
        util.exportExcel(response, list, "cmc员工绩效数据");
    }

    /**
     * 获取cmc员工绩效详细信息
     */
    @GetMapping(value = "/{performanceStaffId}")
    public AjaxResult getInfo(@PathVariable("performanceStaffId") Long performanceStaffId)
    {
        return success(cmcPerformanceStaffService.selectCmcPerformanceStaffByPerformanceStaffId(performanceStaffId));
    }

    /**
     * 上传员工绩效excel文件
     */
    @PostMapping("/uploadSheet")
    public AjaxResult uploadPerformanceStaffSheet(MultipartFile file, String performanceId) throws Exception {
        if (file.isEmpty()) {
            return AjaxResult.error("文件内容为空！");
        }
        else {
            ExcelUtil<CmcPerformanceStaff> util = new ExcelUtil<CmcPerformanceStaff>(CmcPerformanceStaff.class);
            List<CmcPerformanceStaff> performanceStaffList = util.importExcel(file.getInputStream());
            for (CmcPerformanceStaff performanceStaff : performanceStaffList) {
                performanceStaff.setPerformanceId(performanceId);
                performanceStaff.setWorkload(Math.round(performanceStaff.getWorkload() * 1000) / 1000.0);
                performanceStaff.setUserId(userService.selectUserByNickName(performanceStaff.getNickName()).getUserId());
                CmcProject cmcProject = new CmcProject();
                cmcProject.setProjectNumber(performanceStaff.getProjectNumber());
                if (performanceStaff.getProjectNumber() != null && !performanceStaff.getProjectNumber().equals("0"))
                    if (projectService.selectCmcProjectByProjectNumbers(cmcProject).size() > 0)
                        performanceStaff.setProjectId(projectService.selectCmcProjectByProjectNumbers(cmcProject).get(0).getProjectId());
                else
                    performanceStaff.setProjectId("0");
                CmcPerformanceStaff staff = new CmcPerformanceStaff();
                staff.setUserId(performanceStaff.getUserId());
                staff.setPerformanceId(performanceStaff.getPerformanceId());
                staff.setProjectId(performanceStaff.getProjectId());
                staff.setWorkType(performanceStaff.getWorkType());
                staff.setContent(performanceStaff.getContent());
                staff.setPerformanceMonth(performanceStaff.getPerformanceMonth());
                List<CmcPerformanceStaff> staffList = cmcPerformanceStaffService.selectCmcPerformanceStaffList(staff);
                if (staffList.size() > 0) {
                    staff.setPerformanceStaffId(performanceStaffList.get(0).getPerformanceStaffId());
                    staff.setContent(performanceStaff.getContent());
                    staff.setPrice(performanceStaff.getPrice());
                    staff.setProducePerformance(performanceStaff.getProducePerformance());
                    staff.setCoefficient(performanceStaff.getCoefficient());
                    staff.setScaleGrade(performanceStaff.getScaleGrade());
                    staff.setUnit(performanceStaff.getUnit());
                    staff.setWorkload(performanceStaff.getWorkload());
                    staff.setRemark(performanceStaff.getRemark());
                    cmcPerformanceStaffService.updateCmcPerformanceStaff(staff);
                }
                else
                    cmcPerformanceStaffService.insertCmcPerformanceStaff(performanceStaff);
            }
            return AjaxResult.success("上传成功");
        }
    }

    /**
     * 新增cmc员工绩效
     */
    @Log(title = "cmc员工绩效", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcPerformanceStaff cmcPerformanceStaff)
    {
        return toAjax(cmcPerformanceStaffService.insertCmcPerformanceStaff(cmcPerformanceStaff));
    }

    /**
     * 修改cmc员工绩效
     */
    @Log(title = "cmc员工绩效", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcPerformanceStaff cmcPerformanceStaff)
    {
        return toAjax(cmcPerformanceStaffService.updateCmcPerformanceStaff(cmcPerformanceStaff));
    }

    /**
     * 删除cmc员工绩效
     */
    @Log(title = "cmc员工绩效", businessType = BusinessType.DELETE)
	@DeleteMapping("/{performanceId}")
    public AjaxResult remove(@PathVariable String[] performanceId)
    {
        return success(cmcPerformanceStaffService.deleteCmcPerformanceStaffByPerformanceId(performanceId));
    }
}
