package com.ruoyi.web.controller.oa;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.oa.domain.CmcPriceRemark;
import com.ruoyi.oa.service.ICmcPriceRemarkService;
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.domain.CmcPrice;
import com.ruoyi.oa.service.ICmcPriceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 单价表Controller
 *
 * @author cmc
 * @date 2022-07-14
 */
@RestController
@RequestMapping("/oa/price")
public class CmcPriceController extends BaseController
{
    @Autowired
    private ICmcPriceService cmcPriceService;

    @Autowired
    private ICmcPriceRemarkService cmcPriceRemarkService;

    /**
     * 查询单价表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcPrice cmcPrice)
    {
        startPage();
        List<CmcPrice> list = cmcPriceService.selectCmcPriceList(cmcPrice);
        return getDataTable(list);
    }

    /**
     * 导出单价表列表
     */
    @Log(title = "单价表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcPrice cmcPrice)
    {
        List<CmcPrice> list = cmcPriceService.selectCmcPriceList(cmcPrice);
        ExcelUtil<CmcPrice> util = new ExcelUtil<CmcPrice>(CmcPrice.class);
        util.exportExcel(response, list, "price");
    }

    /**
     * 获取单价表详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cmcPriceService.selectCmcPriceById(id));
    }

    /**
     * 查询单价备注
     */
    @GetMapping("/remark")
    public AjaxResult getPriceRemarkByWorkType(String workType)
    {
        CmcPriceRemark cmcPriceRemark = new CmcPriceRemark();
        cmcPriceRemark.setWorkType(workType);
        CmcPriceRemark priceRemark = new CmcPriceRemark();
        if(cmcPriceRemarkService.selectCmcPriceRemarkList(cmcPriceRemark).size()>0)
            priceRemark= cmcPriceRemarkService.selectCmcPriceRemarkList(cmcPriceRemark).get(0);
        return success(priceRemark.getRemark());
    }

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

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

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

    /**
     * 根据工作内容查询比例等级
     */
    @GetMapping( "/scaleGrade")
    public List<String> getScaleGradeInfo(String workItem)
    {
        List<String> scaleGradeList = new ArrayList<>();
        List<CmcPrice> list = cmcPriceService.selectCmcPriceByWorkItem(workItem);
        for (CmcPrice cmcPrice : list)
            scaleGradeList.add(cmcPrice.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<CmcPrice> cmcPriceList = cmcPriceService.selectCmcPriceByScaleGrade(workItem, subItem, scaleGrade);
        JSONObject price = new JSONObject();
        price.put("id", cmcPriceList.get(0).getId());
        price.put("unit", cmcPriceList.get(0).getUnit());
        if (groundType.equals("0"))
            price.put("price", cmcPriceList.get(0).getCommonPrice());
        else
            price.put("price", cmcPriceList.get(0).getComplexPrice());
        return success(price);
    }

    /**
     * 新增单价表
     */
    @Log(title = "单价表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcPrice cmcPrice)
    {
        return toAjax(cmcPriceService.insertCmcPrice(cmcPrice));
    }

    /**
     * 修改单价表
     */
    @Log(title = "单价表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcPrice cmcPrice)
    {
        return toAjax(cmcPriceService.updateCmcPrice(cmcPrice));
    }

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