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 com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.address.entity.TbAddress;
import org.jeecg.modules.address.service.ITbAddressService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.postBean.ProductOrderReq;
import org.jeecg.modules.postBean.UpdateOrderReq;
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.tbSpecs.entity.TbSpecs;
import org.jeecg.modules.tbSpecs.service.ITbSpecsService;
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 org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @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;
    @Resource
    private ITbAddressService addressService;
    @Resource
    private ITbSpecsService tbSpecsService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result 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);
        if (ValidateTool.isNull(one1)) {
            return Result.error("身份审核未通过");
        }
        TbAddress byId = addressService.getById(productReq.getAddressId());
        if (ValidateTool.isNull(byId)) {
            return Result.error("请选择地址");
        }
//        Object productPic = redisUtil.get("productPic");
        TbSpecs specs = tbSpecsService.getById(productReq.getSpecsId());
        if (ValidateTool.isNull(specs)) {
            return Result.error("请选择规格");
        }
        TbProduct product = new TbProduct();
        BeanUtils.copyProperties(productReq, product);
        product.setUserId(hanHaiMember.getId());
        product.setCompanyName(one1.getCompanyName());
        product.setAuditStatus(0);
        product.setAddress(byId.getAddress()+byId.getAddressDetail());
        product.setShowStatus(2);
        //默认图片
        product.setProductType(2);
        product.setSpecsName(specs.getSpecsName());
        tbProductMapper.insert(product);
        return Result.OK("添加成功！");
    }

    @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 product = tbProductService.getOne(productQueryWrapper);
        if (ValidateTool.isNull(product)) {
            return Result.error("订单信息错误");
        }
        if (product.getAuditStatus() != 1 || product.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);
        if (ValidateTool.isNull(productReq.getNum())) {
            return Result.error("请选择数量");
        }
        if (productReq.getNum() > product.getNum()) {
            return Result.error("产品数量不足");
        }
//        总价
        BigDecimal multiply = new BigDecimal(productReq.getNum()).multiply(product.getPrice());
        tbProductOrder.setId(IdWorker.getIdStr());
        tbProductOrder.setDeposit(multiply.multiply(new BigDecimal("0.1")));
        tbProductOrder.setAddress(product.getAddress());
        tbProductOrder.setAddressId(product.getAddressId());
        tbProductOrder.setBuyUserId(hanHaiMember.getId());
        tbProductOrder.setProductId(productReq.getProductId());
        tbProductOrder.setUserName(one2.getCompanyName());
        tbProductOrder.setPhone(one2.getPhone());
        tbProductOrder.setPic(product.getPic());
        tbProductOrder.setPicDetail(product.getPicDetail());
        tbProductOrder.setVideo(product.getVideo());
        tbProductOrder.setReportVideo(product.getReportVideo());
        tbProductOrder.setSpecsId(product.getSpecsId());
        tbProductOrder.setSpecsName(product.getSpecsName());
        tbProductOrder.setPrice(product.getPrice());
        tbProductOrder.setTakeTime(productReq.getTakeTime());
        LambdaQueryWrapper<TbUserRole> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(TbUserRole::getUserId, product.getUserId()).
                eq(TbUserRole::getRole, 1).eq(TbUserRole::getAuditStatus, 1);
        TbUserRole one1 = tbUserRoleService.getOne(objectLambdaQueryWrapper);
        JSONObject jsonObject = new JSONObject();
        tbProductOrder.setCompanyName(one1.getCompanyName());
        tbProductOrder.setBankName(one1.getBankNama());
        tbProductOrder.setProductType(product.getProductType());
        tbProductOrder.setAuditStatus(0);
        tbProductOrder.setBankAccount(one1.getBankAccount());
        jsonObject.put("bankAccount", one1.getBankAccount());
        jsonObject.put("companyName", one1.getCompanyName());
        jsonObject.put("bankName", one1.getBankNama());
        jsonObject.put("orderId", tbProductOrder.getId());
        tbProductOrderService.save(tbProductOrder);
        return Result.OK(jsonObject);
    }

    @Transactional
    @Override
    public Result updateOrder(UpdateOrderReq updateOrderReq, String token) {
        TbProductOrder order = tbProductOrderService.getById(updateOrderReq.getOrderId());
        if (ValidateTool.isNull(order)) {
            return Result.error("订单信息错误");
        }
        if (updateOrderReq.getType() == 0) {
            order.setOrderFlag(1);
        }
        if (updateOrderReq.getType() == 1) {
            order.setOrderFlag(2);
        }
        tbProductOrderService.updateById(order);
        return Result.OK();
    }
}
