package com.ruoyi.web.controller;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.text.Convert;
import io.swagger.annotations.ApiOperation;
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.web.domain.CgInfo;
import com.ruoyi.web.service.ICgInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购信息Controller
 * 
 * @author shenzx
 * @date 2025-09-28
 */
@RestController
@RequestMapping("/cg/info")
public class CgInfoController extends BaseController
{
    private ICgInfoService cgInfoService;
    @Autowired
    public void setCgInfoService(ICgInfoService cgInfoService) {
        this.cgInfoService = cgInfoService;
    }

    @GetMapping("cgpc")
    @ApiOperation("获取采购批次")
    public AjaxResult cgpc(String splx){
        QueryWrapper<CgInfo> qw = new QueryWrapper<>();
        qw.lambda().eq(CgInfo::getSplx,splx).orderByDesc(CgInfo::getCgsj);
        return success(cgInfoService.list(qw));
    }

    /**
     * 查询采购信息全部列表
     */
    @GetMapping("/all")
    public AjaxResult all(CgInfo cgInfo)
    {
        startOrderBy();
        QueryWrapper<CgInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(cgInfo);
        List<CgInfo> cgInfoList = cgInfoService.list(queryWrapper);
        return success(cgInfoList);
    }

    /**
     * 查询采购信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CgInfo cgInfo)
    {
        startPage();
        QueryWrapper<CgInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(cgInfo);
        List<CgInfo> cgInfoList = cgInfoService.list(queryWrapper);
        return getDataTable(cgInfoList);
    }

    /**
     * 导出采购信息列表
     */
    @Log(title = "采购信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CgInfo cgInfo)
    {
        QueryWrapper<CgInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(cgInfo);
        List<CgInfo> list = cgInfoService.list(queryWrapper);
        ExcelUtil<CgInfo> util = new ExcelUtil<>(CgInfo.class);
        util.exportExcel(response, list, "采购信息数据");
    }

    /**
     * 获取采购信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        CgInfo cgInfo = cgInfoService.getById(id);
        return success(cgInfo);
    }

    /**
     * 新增采购信息
     */
    @Log(title = "采购信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CgInfo cgInfo)
    {
        cgInfo.setCgpc(genCgpc(cgInfo.getSplx()));
        cgInfoService.save(cgInfo);
        return success(cgInfo);
    }

    private String genCgpc(String splx){
        LocalDateTime now = LocalDateTime.now();
        DecimalFormat splxFormat = new DecimalFormat("00");
        DecimalFormat xhFormat = new DecimalFormat("0000");
        QueryWrapper<CgInfo> qw = new QueryWrapper<>();
        qw.lambda().select(CgInfo::getCgpc).likeLeft(CgInfo::getCgpc,now.getYear() + splxFormat.format(splx)).orderByDesc(CgInfo::getCgpc);
        List<CgInfo> list = cgInfoService.list(qw);
        if (list.isEmpty()) return now.getYear() + splxFormat.format(splx) + xhFormat.format("1");
        String cgpc = list.get(0).getCgpc();
        int newXh = Convert.toInt(cgpc.substring(cgpc.length() - 4)) + 1;
        return now.getYear() + splxFormat.format(splx) + xhFormat.format(newXh);
    }

    /**
     * 修改采购信息
     */
    @Log(title = "采购信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CgInfo cgInfo)
    {
        cgInfoService.updateById(cgInfo);
        return success(cgInfo);
    }

    /**
     * 删除采购信息
     */
    @Log(title = "采购信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cgInfoService.removeByIds(Arrays.asList(ids)));
    }
}
