package com.ruoyi.web.base.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.BaseLine;
import com.ruoyi.web.base.domain.BaseMarket;
import com.ruoyi.web.base.domain.param.LineRequest;
import com.ruoyi.web.base.domain.param.WiteStripRequest;
import com.ruoyi.web.base.service.IBaseLineService;
import com.ruoyi.web.base.service.IBaseMarketService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * <p>
 * 物流线 前端控制器
 * </p>
 *
 * @author author
 * @since 2026-02-03
 */
@RestController
@RequestMapping("/base-line")
public class BaseLineController {

    @Autowired
    private IBaseLineService baseLineService;
    @Autowired
    private IBaseMarketService marketService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("物流线管理添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BaseLine baseLine, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (baseLineService.count(new QueryWrapper<BaseLine>().eq("org_id",loginOrgId).eq("line_num", baseLine.getLineNum())) >0 ){
            throw new Exception("该编号已存在");
        }

        baseLine.setOrgId(loginOrgId);
        baseLine.setCreateBy(getUsername());
        return success(baseLineService.save(baseLine));
    }

    @ApiOperation("物流线管理修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BaseLine baseLine, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (baseLineService.count(new QueryWrapper<BaseLine>().ne("id",baseLine.getId()).eq("org_id",loginOrgId).eq("line_num", baseLine.getLineNum())) >0 ){
            throw new Exception("该编号已存在");
        }
        baseLine.setUpdateBy(getUsername());
        return success(baseLineService.updateById(baseLine));
    }

    @ApiOperation("物流线管理删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String s : ids.split(",")) {
            baseLineService.removeById(s);
        }
        return success();
    }

    @ApiOperation("物流线管理列表")
    @PostMapping("/list")
    public AjaxResult listAll( HttpServletRequest request){
        return success(baseLineService.list(new QueryWrapper<BaseLine>().eq("org_id",tokenService.getLoginOrgId(request))));
    }

    @ApiOperation("物流线管理分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,HttpServletRequest request
    ) {
        return success(baseLineService.page(new Page<BaseLine>(pageNum,pageSize),new QueryWrapper<BaseLine>().eq("org_id",tokenService.getLoginOrgId(request))));
    }
    @ApiOperation("物流线市场分页")
    @PostMapping("/lineMarketPage")
    public AjaxResult lineMarketPage(
            @RequestBody LineRequest lineRequest,
            HttpServletRequest request
    ) {
        QueryWrapper<BaseLine> wrapper = new QueryWrapper<BaseLine>().eq("org_id", tokenService.getLoginOrgId(request));
        Page<BaseLine> page = baseLineService.page(new Page<BaseLine>(lineRequest.getPageNum(), lineRequest.getPageSize()),
                wrapper);
        for (BaseLine record : page.getRecords()) {
            record.setGoods(marketService.list(new QueryWrapper<BaseMarket>().eq("line_num",record.getLineNum())));
        }
        return success(page );
    }

    @ApiOperation("物流线管理详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        return success(baseLineService.getById(id));
    }

}
