package org.jeecg.modules.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
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.mapper.TbProductMapper;
import org.jeecg.modules.product.service.ITbProductService;
import org.jeecg.modules.product.service.ProductService;
import org.jeecg.modules.productOrder.entity.TbProductOrder;
import org.jeecg.modules.productOrder.service.ITbProductOrderService;
import org.jeecg.modules.userRole.entity.TbUserRole;
import org.jeecg.modules.userRole.service.ITbUserRoleService;
import org.jeecg.modules.utils.ValidateTool;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Description: 产品报价表
 * @Author: jeecg-boot
 * @Date:   2024-10-19
 * @Version: V1.0
 */
@Service
public class ProductServiceImpl extends ServiceImpl<TbProductMapper, TbProduct> implements ProductService {
    @Resource
    private ShiroRealm shiroRealm;
    @Resource
    private ITbProductService tbProductService;
    @Resource
    private ITbUserRoleService tbUserRoleService;
    @Resource
    private TbProductMapper tbProductMapper;
    @Resource
    private ITbProductOrderService tbProductOrderService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addProduct(ProductReq productReq,String token) {

        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        LambdaQueryWrapper<TbUserRole> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(TbUserRole::getUserId,hanHaiMember.getId()).
                eq(TbUserRole::getRole,1).eq(TbUserRole::getAuditStatus,1);
        TbUserRole one1 = tbUserRoleService.getOne(objectLambdaQueryWrapper);
        TbProduct product = new TbProduct();
        BeanUtils.copyProperties(productReq, product);
        product.setUserId(hanHaiMember.getId());
        product.setCompanyName(one1.getCompanyName());
        product.setAuditStatus(0);
        product.setShowStatus(2);
        product.setProductType(2);
        tbProductMapper.insert(product);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result addProductOrder(ProductOrderReq productReq, String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        QueryWrapper<TbProduct> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("id", productReq.getProductId());
        TbProduct one = tbProductService.getOne(productQueryWrapper);
        if (ValidateTool.isNull(one)){
            return Result.error("订单信息错误");
        }
        if (one.getAuditStatus()!=1 || one.getShowStatus()!=0){
            return Result.error("订单状态异常，请刷新");
        }
        QueryWrapper<TbUserRole> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("user_id",hanHaiMember.getId()).eq("role",0);
        TbUserRole one2 = tbUserRoleService.getOne(objectQueryWrapper);
        TbProductOrder tbProductOrder = new TbProductOrder();
        BeanUtils.copyProperties(productReq, tbProductOrder);
        tbProductOrder.setBuyUserId(one.getBuyUserId());
        tbProductOrder.setProductId(productReq.getProductId());
        tbProductOrder.setUserName(one2.getCompanyName());
        tbProductOrder.setPhone(one.getPhone());
            LambdaQueryWrapper<TbUserRole> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(TbUserRole::getUserId,one.getUserId()).
                eq(TbUserRole::getRole,1).eq(TbUserRole::getAuditStatus,1);
        TbUserRole one1 = tbUserRoleService.getOne(objectLambdaQueryWrapper);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bankAccount",one1.getBankAccount());
        jsonObject.put("companyName",one1.getCompanyName());
        jsonObject.put("bankNama",one1.getBankNama());
        tbProductOrderService.save(tbProductOrder);
        return Result.OK(jsonObject);
    }
}
