package com.ruoyi.web.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.ProcureQuotation;
import com.ruoyi.web.base.domain.ProcureQuotationGoods;
import com.ruoyi.web.base.service.IProcureQuotationService;
import com.ruoyi.web.base.service.IProcureQuotationGoodsService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;
import static com.ruoyi.web.base.util.NumUtils.generateString;
import static com.ruoyi.web.base.util.NumUtils.substringToInt;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
 * <p>
 * 供应商报价单 前端控制器
 * </p>
 *
 * @author author
 * @since 2026-02-05
 */
@RestController
@RequestMapping("/procure-quotation")
public class ProcureQuotationController {
    @Autowired
    private IProcureQuotationService procureQuotationService;
    @Autowired
    private IProcureQuotationGoodsService goodsService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("供应商报价单添加")
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@RequestBody ProcureQuotation procureQuotation, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (procureQuotationService.count(new QueryWrapper<ProcureQuotation>().eq("org_id",loginOrgId).eq("quotation_num", procureQuotation.getQuotationNum())) >0 ){
            throw new Exception("该编号已存在");
        }
        String username = getUsername();
        procureQuotation.setOrgId(loginOrgId);
        procureQuotation.setCreateBy(username);
        List<ProcureQuotationGoods> goods = procureQuotation.getGoods();
        for (ProcureQuotationGoods good : goods) {
            good.setOrgId(loginOrgId);
            good.setCreateBy(username);
            good.setQuotationNum(procureQuotation.getQuotationNum());
            goodsService.save(good);
        }

        return success(procureQuotationService.save(procureQuotation));
    }

    @ApiOperation("供应商报价单修改")
    @PostMapping("/edit")
    @Transactional
    public AjaxResult edit(@RequestBody ProcureQuotation procureQuotation, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (procureQuotationService.count(new QueryWrapper<ProcureQuotation>().ne("id",procureQuotation.getId()).eq("org_id",loginOrgId).eq("quotation_num", procureQuotation.getQuotationNum())) >0 ){
            throw new Exception("该编号已存在");
        }

        String username = getUsername();
        procureQuotation.setOrgId(loginOrgId);
        procureQuotation.setCreateBy(username);
        List<ProcureQuotationGoods> goods = procureQuotation.getGoods();
        goodsService.remove(new QueryWrapper<ProcureQuotationGoods>().eq("quotation_num",procureQuotation.getQuotationNum()));
        for (ProcureQuotationGoods good : goods) {
            good.setOrgId(loginOrgId);
            good.setCreateBy(username);
            good.setQuotationNum(procureQuotation.getQuotationNum());
            goodsService.save(good);
        }
        procureQuotation.setUpdateBy(username);

        return success(procureQuotationService.updateById(procureQuotation));
    }

    @ApiOperation("供应商报价单删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String s : ids.split(",")) {
            procureQuotationService.removeById(s);
        }
        return success();
    }

    @ApiOperation("供应商报价单列表")
    @PostMapping("/list")
    public AjaxResult listAll( HttpServletRequest request){
        List<ProcureQuotation> org_id = procureQuotationService.list(new QueryWrapper<ProcureQuotation>().eq("org_id", tokenService.getLoginOrgId(request)));
        for (ProcureQuotation procureQuotation : org_id) {
            procureQuotation.setGoods(goodsService.list(new QueryWrapper<ProcureQuotationGoods>().eq("quotation_num",procureQuotation.getQuotationNum())));
        }
        return success(org_id);
    }

    @ApiOperation("供应商报价单分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , HttpServletRequest request) {
        Page<ProcureQuotation> org_id = procureQuotationService.page(new Page<ProcureQuotation>(pageNum, pageSize), new QueryWrapper<ProcureQuotation>().eq("org_id", tokenService.getLoginOrgId(request)));
        for (ProcureQuotation procureQuotation : org_id.getRecords()) {
            procureQuotation.setGoods(goodsService.list(new QueryWrapper<ProcureQuotationGoods>().eq("quotation_num",procureQuotation.getQuotationNum())));
        }
        return success(org_id);
    }

    @ApiOperation("供应商报价单详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        ProcureQuotation byId = procureQuotationService.getById(id);
        byId.setGoods(goodsService.list(new QueryWrapper<ProcureQuotationGoods>().eq("quotation_num",byId.getQuotationNum())));
        return success(byId);
    }

    @ApiOperation("获取最新报价单编号")
    @PostMapping("/getQuotaNum")
    public AjaxResult getQuotaNum(HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        ProcureQuotation one = procureQuotationService.getOne(new QueryWrapper<ProcureQuotation>().eq("org_id", loginOrgId).orderByDesc("id").last("limit 1"));
        if (ObjectUtils.isEmpty(one)){

            return success(generateString("bj",1));
        }
        String quotationNum = one.getQuotationNum();

        return success(generateString("bj",substringToInt(quotationNum)));

    }


}
