package com.ruoyi.web.controller.assessment;


import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.assessment.ZxAssessment;
import org.apache.commons.lang3.ArrayUtils;
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.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.system.service.IZxAssessmentService;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 政协评价考核Controller
 *
 * @author boman
 * @date 2024-03-19
 */
@RestController
@RequestMapping("/zx/assessment")
public class ZxAssessmentController extends BaseController {
    @Autowired
    private IZxAssessmentService zxAssessmentService;

    /**
     * 查询政协评价考核列表
     */
    @PreAuthorize("@ss.hasPermi('zxAssessment:assessment:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZxAssessment zxAssessment) {

        List<ZxAssessment> list = zxAssessmentService.selectZxAssessmentList(zxAssessment);
        return getDataTable(list);
    }

    /**
     * 查询政协评价考核列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('zxAssessment:assessment:list')")
    @GetMapping("/list/exclude/{assessmentId}")
    public AjaxResult excludeChild(@PathVariable(value = "assessmentId", required = false) Long assessmentId) {
        List<ZxAssessment> zxAssessments = zxAssessmentService.selectZxAssessmentList(new ZxAssessment());
        zxAssessments.removeIf(d -> d.getAssessmentId().intValue() == assessmentId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), assessmentId + ""));
        return success(zxAssessments);
    }

    /**
     * 获取政协评价考核详细信息
     */
    @PreAuthorize("@ss.hasPermi('zxAssessment:assessment:query')")
    @GetMapping(value = "/{assessmentId}")
    public AjaxResult getInfo(@PathVariable("assessmentId") Long assessmentId) {
        return success(zxAssessmentService.selectZxAssessmentByAssessmentId(assessmentId));
    }

    /**
     * 新增政协评价考核
     */
    @PreAuthorize("@ss.hasPermi('zxAssessment:assessment:add')")
    @Log(title = "政协评价考核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZxAssessment zxAssessment) {
        if (!zxAssessmentService.checkAssessmentProjectUnique(zxAssessment)) {
            return error("新增考核规则'" + zxAssessment.getAssessmentProject() + "'失败，考核规则已存在");
        }
        return toAjax(zxAssessmentService.insertZxAssessment(zxAssessment));
    }

    /**
     * 修改政协评价考核
     */
    @PreAuthorize("@ss.hasPermi('zxAssessment:assessment:edit')")
    @Log(title = "政协评价考核", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody ZxAssessment zxAssessment) {
/*        if (!zxAssessmentService.checkAssessmentProjectUnique(zxAssessment)) {
            return error("修改考核规则'" + zxAssessment.getAssessmentProject() + "'失败，考核规则已存在");
        }*/
        return toAjax(zxAssessmentService.updateZxAssessment(zxAssessment));
    }

    /**
     * 删除政协评价考核
     */
    @PreAuthorize("@ss.hasPermi('zxAssessment:assessment:remove')")
    @Log(title = "政协评价考核", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{assessmentIds}")
    public AjaxResult remove(@PathVariable Long[] assessmentIds) {
        return toAjax(zxAssessmentService.deleteZxAssessmentByAssessmentIds(assessmentIds));
    }
}
