package org.jeecg.modules.alUser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.postBean.CustomsReq;
import org.jeecg.modules.postBean.ProductOrderReq;
import org.jeecg.modules.product.entity.ProductReq;
import org.jeecg.modules.product.entity.TbProduct;
import org.jeecg.modules.product.service.ProductService;
import org.jeecg.modules.productOrder.entity.TbProductOrder;
import org.jeecg.modules.tbCustoms.entity.TbCustoms;
import org.jeecg.modules.tbCustoms.service.ITbCustomsService;
import org.jeecg.modules.utils.ValidateTool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.jeecg.qywx.api.base.JwParamesAPI.token;

/**
 * @Description: 产品报价表
 * @Author: jeecg-boot
 * @Date: 2024-10-19
 * @Version: V1.0
 */
@Api(tags = "产品报价")
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController extends JeecgController<TbProduct, ProductService> {
    @Autowired
    private ProductService tbProductService;
    @Resource
    private ShiroRealm shiroRealm;

    @Resource
    private ITbCustomsService tbCustomsService;
    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "产品报价分页列表", notes = "产品报价分页列表")
    @GetMapping(value = "/productList")
    public Result<IPage<TbProduct>> queryPageList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        QueryWrapper<TbProduct> queryWrapper = QueryGenerator.initQueryWrapper(new TbProduct(), req.getParameterMap());
        queryWrapper.eq("audit_status", 1)
                .orderByDesc("sort_num", "create_time");
        Page<TbProduct> page = new Page<TbProduct>(pageNo, pageSize);
        IPage<TbProduct> pageList = tbProductService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @ApiOperation(value = "交易平台挂单列表", notes = "交易平台挂单列表")
    @GetMapping(value = "/productlist")
    public Result<IPage<TbProduct>> productlist(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        QueryWrapper<TbProduct> queryWrapper = QueryGenerator.initQueryWrapper(new TbProduct(), req.getParameterMap());
        queryWrapper.eq("show_status", 0).eq("audit_status", 1)
                .orderByDesc("sort_num", "create_time");
        Page<TbProduct> page = new Page<TbProduct>(pageNo, pageSize);
        IPage<TbProduct> pageList = tbProductService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @ApiOperation(value = "我的挂单列表", notes = "我的挂单列表")
    @GetMapping(value = "/myProductlist")
    public Result<IPage<TbProduct>> myProductlist(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req, @RequestHeader("X-Access-Token") String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);

        QueryWrapper<TbProduct> queryWrapper = QueryGenerator.initQueryWrapper(new TbProduct(), req.getParameterMap());
        queryWrapper.eq("user_id", hanHaiMember.getId()).orderByDesc("create_time");
        Page<TbProduct> page = new Page<TbProduct>(pageNo, pageSize);
        IPage<TbProduct> pageList = tbProductService.page(page, queryWrapper);
        return Result.OK(pageList);
    }




    @ApiOperation(value="撤单", notes="撤单")
    @GetMapping(value = "/noShow")
    public Result<String> noShow(@RequestParam(name="id",required=true ) String id , @RequestHeader("X-Access-Token") String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        QueryWrapper<TbProduct> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("user_id", hanHaiMember.getId()).eq("id", id);
        TbProduct one = tbProductService.getOne(productQueryWrapper);
        if (ValidateTool.isNull(one)){
            return Result.error("参数错误!");
        }
        one.setShowStatus(1);
        tbProductService.updateById(one);
        return Result.OK("撤单成功!");
    }



    /**
     * 添加
     *
     * @param
     * @return
     */
    @ApiOperation(value = "产品报价", notes = "产品报价")
    @PostMapping(value = "/addProduct")
    public Result<String> addProduct(@RequestBody ProductReq productReq, @RequestHeader("X-Access-Token") String token) {
        tbProductService.addProduct(productReq, token);
        return Result.OK("添加成功！");
    }


    @ApiOperation(value="清关申请", notes="清关申请")
    @PostMapping(value = "/addCustoms")
    public Result<String> addCustoms(@RequestBody CustomsReq customsReq, @RequestHeader("X-Access-Token") String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        TbCustoms tbCustoms = new TbCustoms();
        BeanUtils.copyProperties(customsReq, tbCustoms);
        tbCustoms.setUserId(hanHaiMember.getId());
        tbCustomsService.save(tbCustoms);
        return Result.OK("添加成功！");
    }


    @ApiOperation(value="下订单", notes="订单")
    @PostMapping(value = "/addProductOrder")
    public Result<String> addProductOrder(@RequestBody ProductOrderReq productOrderReq, @RequestHeader("X-Access-Token") String token) {
        if (ValidateTool.isNull(productOrderReq)){
            return Result.error("参数不能为空!");
        }

        Result result = tbProductService.addProductOrder(productOrderReq, token);
        return result;
    }


}
