package com.ruoyi.web.controller.clMaterial;

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

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
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.ruoyi.clMaterial.domain.CYlMaterial;
import com.ruoyi.clMaterial.service.ICYlMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 材料管理Controller
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@RestController
@RequestMapping("/clMaterial/clMaterial")
@DataSource(DataSourceType.QUOT)
public class CYlMaterialController extends BaseController
{
    @Autowired
    private ICYlMaterialService cYlMaterialService;

    /**
     * 查询材料管理列表
     */
    @PreAuthorize("@ss.hasPermi('clMaterial:clMaterial:list')")
    @GetMapping("/list")
    public TableDataInfo list(CYlMaterial cYlMaterial)
    {
        startPage();
        List<CYlMaterial> list = cYlMaterialService.selectCYlMaterialList(cYlMaterial);
        return getDataTable(list);
    }

    /**
     * 导出材料管理列表
     */
    @PreAuthorize("@ss.hasPermi('clMaterial:clMaterial:export')")
    @Log(title = "材料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CYlMaterial cYlMaterial)
    {
        List<CYlMaterial> list = cYlMaterialService.selectCYlMaterialList(cYlMaterial);
        ExcelUtil<CYlMaterial> util = new ExcelUtil<CYlMaterial>(CYlMaterial.class);
        util.exportExcel(response, list, "材料管理数据");
    }

    /**
     * 获取材料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('clMaterial:clMaterial:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return success(cYlMaterialService.selectCYlMaterialByMaterialId(materialId));
    }

    /**
     * 新增材料管理
     */
    @PreAuthorize("@ss.hasPermi('clMaterial:clMaterial:add')")
    @Log(title = "材料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CYlMaterial cYlMaterial)
    {
        if (!cYlMaterialService.checkMaterialNoUnique(cYlMaterial))
        {
            return error("添加材料失败,编码:'" + cYlMaterial.getMaterialNo() + "' 已存在");
        }
        return toAjax(cYlMaterialService.insertCYlMaterial(cYlMaterial));
    }

    /**
     * 修改材料管理
     */
    @PreAuthorize("@ss.hasPermi('clMaterial:clMaterial:edit')")
    @Log(title = "材料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CYlMaterial cYlMaterial)
    {
        return toAjax(cYlMaterialService.updateCYlMaterial(cYlMaterial));
    }

    /**
     * 删除材料管理
     */
    @PreAuthorize("@ss.hasPermi('clMaterial:clMaterial:remove')")
    @Log(title = "材料管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds)
    {
        return toAjax(cYlMaterialService.deleteCYlMaterialByMaterialIds(materialIds));
    }

    /**
     * 同步更细报价实时价格
     */
    @PreAuthorize("@ss.hasPermi('clMaterial:clMaterial:synchronizeQuotationData')")
    @Log(title = "同步更细报价实时价格", businessType = BusinessType.UPDATE)
    @PostMapping("/synchronizeUpdate")
    public AjaxResult synchronizeUpdate()
    {
        //拉取9.2 jn_erp  报价实时价格数据,保存至 c_yl_material_bjdata 表
        List<CYlMaterial> list = cYlMaterialService.getBjData();
        if(list!=null&&list.size()>0){
            cYlMaterialService.deleteYlMaterialBjBatch();
            cYlMaterialService.insertYlMaterialBjBatch(list);
        }

        //数据执行sql 更新 c_yl_material_bjdata 表中单价字段
        cYlMaterialService.updateYlMaterialBjBatch();

        return success();
    }
}
