package com.yanzhu.jh.project.controller;

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

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.SysRoleEnum;
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.SurProjectInsurance;
import com.yanzhu.jh.project.service.ISurProjectInsuranceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目保险Controller
 * 
 * @author JiangYuQi
 * @date 2023-08-16
 */
@RestController
@RequestMapping("/project/surProjectInsurance")
public class SurProjectInsuranceController extends BaseController
{
    @Autowired
    private ISurProjectInsuranceService surProjectInsuranceService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询项目保险列表
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectInsurance:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurProjectInsurance surProjectInsurance)
    {
        startPage();
        surProjectInsurance.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(surProjectInsurance.getNowRole())){
            surProjectInsurance.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            surProjectInsurance.setNowDept(Convert.toStr(getDeptId()));
        }
        surProjectInsurance.setNowUser(Convert.toStr(getUserId()));
        List<SurProjectInsurance> list = surProjectInsuranceService.selectSurProjectInsuranceList(surProjectInsurance);
        return getDataTable(list);
    }

    /**
     * 查询项目保险列表
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectInsurance:list')")
    @GetMapping("/listv2")
    public TableDataInfo listv2(SurProjectInsurance surProjectInsurance)
    {
        startPage();
        List<SurProjectInsurance> list = surProjectInsuranceService.selectSurProjectInsuranceListv2(surProjectInsurance);
        return getDataTable(list);
    }

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

    /**
     * 获取项目保险详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectInsurance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surProjectInsuranceService.selectSurProjectInsuranceById(id));
    }

    /**
     * 新增项目保险
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectInsurance:add')")
    @Log(title = "项目保险", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurProjectInsurance surProjectInsurance)
    {
        int res = surProjectInsuranceService.insertSurProjectInsurance(surProjectInsurance);
        if(res<0){
            Map<String,Object> data = new HashMap<>();
            data.put("resultCode",res);
            return success(data);
        }else{
            return toAjax(res);
        }
    }

    /**
     * 修改项目保险
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectInsurance:edit')")
    @Log(title = "项目保险", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurProjectInsurance surProjectInsurance)
    {
        return toAjax(surProjectInsuranceService.updateSurProjectInsurance(surProjectInsurance));
    }

    /**
     * 删除项目保险
     */
    @PreAuthorize("@ss.hasPermi('project:surProjectInsurance:remove')")
    @Log(title = "项目保险", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surProjectInsuranceService.deleteSurProjectInsuranceByIds(ids));
    }
}
