package com.ruoyi.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cs.domain.CsProjectRecord;
import com.ruoyi.cs.domain.template.CsProjectRecordTemplate;
import com.ruoyi.cs.domain.vo.CsProjectRecordHTVo;
import com.ruoyi.cs.domain.vo.CsProjectRecordZCVo;
import com.ruoyi.cs.service.ICsProjectRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 项目记录Controller
 *
 * @author ruoyi
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/cs/csProjectRecord")
public class CsProjectRecordController extends BaseController
{
    @Resource
    private ICsProjectRecordService csProjectRecordService;

    /**
     * 查询项目记录列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csProjectRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsProjectRecord csProjectRecord)
    {
        startPage();
        csProjectRecord.setDelFlag("0");
        List<CsProjectRecord> list = csProjectRecordService.selectCsProjectRecordList(csProjectRecord);
        return getDataTable(list);
    }

    /**
     * 导出项目记录列表
     */
    @PreAuthorize("@ss.hasPermi('cs:csProjectRecord:export')")
    @Log(title = "项目记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsProjectRecord csProjectRecord)
    {
        csProjectRecord.setDelFlag("0");
        List<CsProjectRecord> list = csProjectRecordService.selectCsProjectRecordList(csProjectRecord);
        if (StringUtils.equals(csProjectRecord.getProjectType(),"0")){
            ExcelUtil<CsProjectRecordHTVo> util = new ExcelUtil<CsProjectRecordHTVo>(CsProjectRecordHTVo.class);
            List<CsProjectRecordHTVo> copyToList = BeanUtil.copyToList(list, CsProjectRecordHTVo.class);
            util.exportExcel(response, copyToList, "项目记录数据");
        }else if (StringUtils.equals(csProjectRecord.getProjectType(),"1")){
            ExcelUtil<CsProjectRecordZCVo> util = new ExcelUtil<CsProjectRecordZCVo>(CsProjectRecordZCVo.class);
            List<CsProjectRecordZCVo> copyToList = BeanUtil.copyToList(list, CsProjectRecordZCVo.class);
            util.exportExcel(response, copyToList, "项目记录数据");
        }else {
            ExcelUtil<CsProjectRecord> util = new ExcelUtil<CsProjectRecord>(CsProjectRecord.class);
            util.exportExcel(response, list, "项目记录数据");
        }

    }

    /**
     * 导出项目记录列表模版
     */
    @Log(title = "项目记录模版", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response)
    {
        ExcelUtil<CsProjectRecordTemplate> util = new ExcelUtil<CsProjectRecordTemplate>(CsProjectRecordTemplate.class);
        util.exportExcel(response, null, "项目记录数据模版");
    }

    /**
     * 获取项目记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('cs:csProjectRecord:query')")
    @GetMapping(value = "/{projectRecordId}")
    public AjaxResult getInfo(@PathVariable("projectRecordId") String projectRecordId)
    {
        return AjaxResult.success(csProjectRecordService.selectCsProjectRecordByProjectRecordId(projectRecordId));
    }

    /**
     * 新增项目记录
     */
    @PreAuthorize("@ss.hasPermi('cs:csProjectRecord:add')")
    @Log(title = "项目记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsProjectRecord csProjectRecord)
    {
        if(StringUtils.isNotBlank(csProjectRecord.getProjectFunds()) && !isNumber(csProjectRecord.getProjectFunds())){
            return error("请以数字的形式填写项目经费");
        }
        if(StringUtils.isNotBlank(csProjectRecord.getMachinePrice()) && !isNumber(csProjectRecord.getMachinePrice())){
            return error("请以数字的形式填写入账金额");
        }
        return toAjax(csProjectRecordService.insertCsProjectRecord(csProjectRecord));
    }

    /**
     * 修改项目记录
     */
    @PreAuthorize("@ss.hasPermi('cs:csProjectRecord:edit')")
    @Log(title = "项目记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsProjectRecord csProjectRecord)
    {
        if(StringUtils.isNotBlank(csProjectRecord.getProjectFunds()) && !isNumber(csProjectRecord.getProjectFunds())){
            return error("请以数字的形式填写项目经费");
        }
        if(StringUtils.isNotBlank(csProjectRecord.getMachinePrice()) && !isNumber(csProjectRecord.getMachinePrice())){
            return error("请以数字的形式填写入账金额");
        }
        return toAjax(csProjectRecordService.updateCsProjectRecord(csProjectRecord));
    }

    /**
     * 删除项目记录
     */
    @PreAuthorize("@ss.hasPermi('cs:csProjectRecord:remove')")
    @Log(title = "项目记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectRecordIds}")
    public AjaxResult remove(@PathVariable String[] projectRecordIds)
    {
        return toAjax(csProjectRecordService.deleteCsProjectRecordByProjectRecordIds(projectRecordIds));
    }

    /**
     * 判断是否为数字
     *
     * @param str
     * @return
     */
    private boolean isNumber(String str) {
        return str.matches("-?\\d+(\\.\\d+)?");
    }

}
