package com.ruoyi.web.controller.oa;

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

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.oa.domain.CmcCheckPrice;
import org.apache.ibatis.annotations.Param;
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.ICmcCheckPriceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc核算单价Controller
 * 
 * @author cmc
 * @date 2025-05-14
 */
@RestController
@RequestMapping("/oa/checkPrice")
public class CmcCheckPriceController extends BaseController
{
    @Autowired
    private ICmcCheckPriceService cmcCheckPriceService;

    /**
     * 查询cmc核算单价列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcCheckPrice cmcCheckPrice)
    {
        startPage();
        List<CmcCheckPrice> list = cmcCheckPriceService.selectCmcCheckPriceList(cmcCheckPrice);
        return getDataTable(list);
    }

    /**
     * 导出cmc核算单价列表
     */
    @Log(title = "cmc核算单价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcCheckPrice cmcCheckPrice)
    {
        List<CmcCheckPrice> list = cmcCheckPriceService.selectCmcCheckPriceList(cmcCheckPrice);
        ExcelUtil<CmcCheckPrice> util = new ExcelUtil<CmcCheckPrice>(CmcCheckPrice.class);
        util.exportExcel(response, list, "cmc核算单价数据");
    }

    /**
     * 获取cmc核算单价详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cmcCheckPriceService.selectCmcCheckPriceById(id));
    }

    /**
     * 查询工作类别列表
     */
    @GetMapping("/workTypeList")
    public List<String> getWorkTypeList()
    {
        List<String> workTypeList = new ArrayList<>();
        List<CmcCheckPrice> list = cmcCheckPriceService.selectCmcCheckPriceList(new CmcCheckPrice());
        for (CmcCheckPrice cmcCheckPrice : list)
            workTypeList.add(cmcCheckPrice.getWorkType());
        return workTypeList;
    }

    /**
     * 根据工作类别查询工作内容
     */
    @GetMapping("/workItem")
    public List<String> getWorkItemInfo(String workType)
    {
        List<String> workItemList = new ArrayList<>();
        List<CmcCheckPrice> list = cmcCheckPriceService.selectCmcCheckPriceByWorkType(workType);
        for (CmcCheckPrice cmcCheckPrice : list)
            workItemList.add(cmcCheckPrice.getWorkItem());
        return workItemList;
    }

    /**
     * 根据工作内容查询内容细项
     */
    @GetMapping("/subItem")
    public List<String> getSubItemInfo(String workItem)
    {
        List<String> subItemList = new ArrayList<>();
        List<CmcCheckPrice> list = cmcCheckPriceService.selectCmcCheckPriceByWorkItem(workItem);
        for (CmcCheckPrice cmcCheckPrice : list)
            subItemList.add(cmcCheckPrice.getSubItem());
        return subItemList;
    }

    /**
     * 根据工作内容查询比例等级
     */
    @GetMapping( "/scaleGrade")
    public List<String> getScaleGradeInfo(String workItem)
    {
        List<String> scaleGradeList = new ArrayList<>();
        List<CmcCheckPrice> list = cmcCheckPriceService.selectCmcCheckPriceByWorkItem(workItem);
        for (CmcCheckPrice cmcCheckPrice : list)
            scaleGradeList.add(cmcCheckPrice.getScaleGrade());
        return scaleGradeList;
    }

    /**
     * 根据比例等级查询出单位、一般地类、复杂地类
     */
    @GetMapping( "/price")
    public AjaxResult getPriceInfo(@Param("workItem")String workItem, @Param("subItem")String subItem, @Param("scaleGrade")String scaleGrade, @Param("groundType")String groundType)
    {
        List<CmcCheckPrice> cmcCheckPriceList = cmcCheckPriceService.selectCmcCheckPriceByScaleGrade(workItem, subItem, scaleGrade);
        JSONObject price = new JSONObject();
        price.put("id", cmcCheckPriceList.get(0).getId());
        price.put("unit", cmcCheckPriceList.get(0).getUnit());
        price.put("remark", cmcCheckPriceList.get(0).getRemark());
        if (groundType.equals("0"))
            price.put("price", cmcCheckPriceList.get(0).getCommonPrice());
        else
            price.put("price", cmcCheckPriceList.get(0).getComplexPrice());
        return success(price);
    }

    /**
     * 新增cmc核算单价
     */
    @Log(title = "cmc核算单价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcCheckPrice cmcCheckPrice)
    {
        return toAjax(cmcCheckPriceService.insertCmcCheckPrice(cmcCheckPrice));
    }

    /**
     * 修改cmc核算单价
     */
    @Log(title = "cmc核算单价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcCheckPrice cmcCheckPrice)
    {
        return toAjax(cmcCheckPriceService.updateCmcCheckPrice(cmcCheckPrice));
    }

    /**
     * 删除cmc核算单价
     */
    @Log(title = "cmc核算单价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return success(cmcCheckPriceService.deleteCmcCheckPriceByIds(ids));
    }
}
