package com.xingxi.api.service.impl;

import com.xingxi.api.common.UserContext;
import com.xingxi.api.model.CartsRequest;
import com.xingxi.api.model.CartsListResponse;
import com.xingxi.api.model.CartsResponse;
import com.xingxi.api.service.CartsServiceI;
import com.xingxi.business.Carts.domain.Carts;
import com.xingxi.business.Carts.mapper.CartsMapper;
import com.xingxi.common.core.domain.entity.SysUser;
import com.xingxi.common.enums.EDelFlag;
import com.xingxi.common.enums.ERoleKey;
import com.xingxi.common.enums.EYesNo;
import com.xingxi.common.utils.DateUtils;
import com.xingxi.common.utils.StringUtils;
import com.xingxi.master.product.domain.ProdAttrPrice;
import com.xingxi.master.product.domain.ProdPic;
import com.xingxi.master.product.mapper.ProdAttrPriceMapper;
import com.xingxi.master.product.service.impl.ProdPicServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service("CartsServiceI")
class CartsApiServiceImpl implements CartsServiceI {

    @Autowired
    CartsMapper cartsMapper;
    @Autowired
    ProdAttrPriceMapper prodAttrPriceMapper;
    @Autowired
    private ProdPicServiceImpl prodPicServiceImpl;

    @Override
    public CartsListResponse add(UserContext userContext, CartsRequest request) {

        SysUser currentUser = userContext.getCurrentUser();
        Long userId = currentUser.getUserId();

        Carts cond = new Carts();
        cond.setUserId(userId);
        cond.setProdId(request.getProdId());
        cond.setProdAttrId(request.getProdAttrId());
        cond.setDelFlag(EDelFlag.NO.getVal());

        List<Carts> exiCarts = cartsMapper.selectCartsList(cond);

        Carts carts = new Carts();

        if (StringUtils.isEmpty(exiCarts)) {
            carts.setUserId(userId);
            carts.setProdId(request.getProdId());
            carts.setProdAttrId(request.getProdAttrId());
            carts.setQuantity(1);
            carts.setDelFlag(EDelFlag.NO.getVal());
            carts.setCheckFlag(EYesNo.YES.getVal());

            cartsMapper.insertCarts(carts);
        }

        return null;
    }

    @Override
    public CartsListResponse list(UserContext userContext, CartsRequest request) {

        SysUser currentUser = userContext.getCurrentUser();
        Long userId = currentUser.getUserId();

        Carts cond = new Carts();
        cond.setUserId(userId);
        cond.setDelFlag(EDelFlag.NO.getVal());

        List<Carts> exiCarts = cartsMapper.selectCartsList(cond);

        BigDecimal sum = BigDecimal.ZERO;

        List<CartsResponse> cartsResponseList = new ArrayList<>();

        for (Carts carts : exiCarts) {

            CartsResponse cartsResponse = new CartsResponse();

            cartsResponse.setId(carts.getId());
            cartsResponse.setUserId(userId);
            cartsResponse.setProdId(carts.getProdId());
            cartsResponse.setProdAttrId(carts.getProdAttrId());
            cartsResponse.setQuantity(carts.getQuantity());
            cartsResponse.setCheckFlag(carts.getCheckFlag());

            ProdPic condPic = new ProdPic();
            condPic.setProdId(carts.getProdId());
            condPic.setDelFlag(EDelFlag.NO.getVal());

            List<ProdPic> picList = prodPicServiceImpl.selectProdPicList(condPic);

            cartsResponse.setProdPicList(picList);

            ProdAttrPrice condPrice = new ProdAttrPrice();
            condPrice.setProdAttrId(carts.getProdAttrId());
            condPrice.setProdId(carts.getProdId());
            condPrice.setBuyerRoleKey(ERoleKey.CUSTOMER.getCode());

            List<ProdAttrPrice> priceList = prodAttrPriceMapper.selectProdAttrPriceList(condPrice);

            if (StringUtils.isNotEmpty(priceList)) {
                if (carts.getCheckFlag().equals(EYesNo.YES.getVal())) {
                    sum = sum.add(new BigDecimal(carts.getQuantity()).multiply(priceList.get(0).getPrice()));
                }
            }
            cartsResponseList.add(cartsResponse);
        }
        return new CartsListResponse(cartsResponseList, sum.toPlainString());
    }

    @Override
    public CartsListResponse checked(UserContext userContext, CartsRequest request) {

        SysUser currentUser = userContext.getCurrentUser();
        Long userId = currentUser.getUserId();

        Carts updCarts = cartsMapper.selectCartsById(request.getId());

        updCarts.setCheckFlag(EYesNo.YES.getVal().equals(updCarts.getCheckFlag()) ? EYesNo.NO.getVal() : EYesNo.YES.getVal());
        updCarts.setUpdateUser(userContext.getCurrentUser().getUserId().toString());
        updCarts.setUpdateTime(DateUtils.getNowDate());
        cartsMapper.updateCarts(updCarts);

        Carts cond = new Carts();
        cond.setUserId(userId);
        cond.setDelFlag(EDelFlag.NO.getVal());

        List<CartsResponse> cartsResponseList = new ArrayList<>();
        List<Carts> exiCarts = cartsMapper.selectCartsList(cond);
        BigDecimal sum = BigDecimal.ZERO;
        for (Carts carts : exiCarts) {
            ProdAttrPrice condPrice = new ProdAttrPrice();
            condPrice.setProdAttrId(carts.getProdAttrId());
            condPrice.setProdId(carts.getProdId());
            condPrice.setBuyerRoleKey(ERoleKey.CUSTOMER.getCode());

            List<ProdAttrPrice> priceList = prodAttrPriceMapper.selectProdAttrPriceList(condPrice);

            if (StringUtils.isNotEmpty(priceList)) {
                if (carts.getCheckFlag().equals(EYesNo.YES.getVal())) {
                    sum = sum.add(new BigDecimal(carts.getQuantity()).multiply(priceList.get(0).getPrice()));
                }
            }
            CartsResponse cartsResponse = new CartsResponse();

            cartsResponse.setId(carts.getId());
            cartsResponse.setUserId(userId);
            cartsResponse.setProdId(carts.getProdId());
            cartsResponse.setProdAttrId(carts.getProdAttrId());
            cartsResponse.setQuantity(carts.getQuantity());
            cartsResponse.setCheckFlag(carts.getCheckFlag());

            ProdPic condPic = new ProdPic();
            condPic.setProdId(carts.getProdId());
            condPic.setDelFlag(EDelFlag.NO.getVal());

            List<ProdPic> picList = prodPicServiceImpl.selectProdPicList(condPic);

            cartsResponse.setProdPicList(picList);

        }

        return new CartsListResponse(cartsResponseList, sum.toPlainString());
    }

    @Override
    public CartsListResponse checkedAll(UserContext userContext, CartsRequest request) {
        SysUser currentUser = userContext.getCurrentUser();
        Long userId = currentUser.getUserId();

        Carts cond = new Carts();
        cond.setUserId(userId);
        cond.setDelFlag(EDelFlag.NO.getVal());

        List<Carts> exiCarts = cartsMapper.selectCartsList(cond);
        List<CartsResponse> cartsResponseList = new ArrayList<>();
        BigDecimal sum = BigDecimal.ZERO;
        for (Carts carts : exiCarts) {
            carts.setCheckFlag(EYesNo.YES.getVal());
            carts.setUpdateUser(userContext.getCurrentUser().getUserId().toString());
            carts.setUpdateTime(DateUtils.getNowDate());
            cartsMapper.updateCarts(carts);

            ProdAttrPrice condPrice = new ProdAttrPrice();
            condPrice.setProdAttrId(carts.getProdAttrId());
            condPrice.setProdId(carts.getProdId());
            condPrice.setBuyerRoleKey(ERoleKey.CUSTOMER.getCode());

            List<ProdAttrPrice> priceList = prodAttrPriceMapper.selectProdAttrPriceList(condPrice);

            if (StringUtils.isNotEmpty(priceList)) {
                sum = sum.add(new BigDecimal(carts.getQuantity()).multiply(priceList.get(0).getPrice()));
            }
            CartsResponse cartsResponse = new CartsResponse();

            cartsResponse.setId(carts.getId());
            cartsResponse.setUserId(userId);
            cartsResponse.setProdId(carts.getProdId());
            cartsResponse.setProdAttrId(carts.getProdAttrId());
            cartsResponse.setQuantity(carts.getQuantity());
            cartsResponse.setCheckFlag(carts.getCheckFlag());

            ProdPic condPic = new ProdPic();
            condPic.setProdId(carts.getProdId());
            condPic.setDelFlag(EDelFlag.NO.getVal());

            List<ProdPic> picList = prodPicServiceImpl.selectProdPicList(condPic);

            cartsResponse.setProdPicList(picList);
        }

        return new CartsListResponse(cartsResponseList, sum.toPlainString());
    }

    @Override
    public CartsListResponse uncheckedAll(UserContext userContext, CartsRequest request) {
        SysUser currentUser = userContext.getCurrentUser();
        Long userId = currentUser.getUserId();

        Carts cond = new Carts();
        cond.setUserId(userId);
        cond.setDelFlag(EDelFlag.NO.getVal());

        List<Carts> exiCarts = cartsMapper.selectCartsList(cond);
        List<CartsResponse> cartsResponseList = new ArrayList<>();

        BigDecimal sum = BigDecimal.ZERO;
        for (Carts carts : exiCarts) {
            carts.setCheckFlag(EYesNo.YES.getVal());
            carts.setUpdateUser(userContext.getCurrentUser().getUserId().toString());
            carts.setUpdateTime(DateUtils.getNowDate());
            cartsMapper.updateCarts(carts);

            CartsResponse cartsResponse = new CartsResponse();

            cartsResponse.setId(carts.getId());
            cartsResponse.setUserId(userId);
            cartsResponse.setProdId(carts.getProdId());
            cartsResponse.setProdAttrId(carts.getProdAttrId());
            cartsResponse.setQuantity(carts.getQuantity());
            cartsResponse.setCheckFlag(carts.getCheckFlag());

            ProdPic condPic = new ProdPic();
            condPic.setProdId(carts.getProdId());
            condPic.setDelFlag(EDelFlag.NO.getVal());

            List<ProdPic> picList = prodPicServiceImpl.selectProdPicList(condPic);
            cartsResponse.setProdPicList(picList);
            cartsResponseList.add(cartsResponse);
        }

        return new CartsListResponse(cartsResponseList, sum.toPlainString());
    }

    @Override
    public CartsListResponse delete(UserContext userContext, CartsRequest request) {

        SysUser currentUser = userContext.getCurrentUser();
        Long userId = currentUser.getUserId();

        cartsMapper.logicDeleteCartsById(request.getId());

        Carts cond = new Carts();
        cond.setUserId(userId);
        cond.setDelFlag(EDelFlag.NO.getVal());

        List<Carts> exiCarts = cartsMapper.selectCartsList(cond);
        List<CartsResponse> cartsResponseList = new ArrayList<>();

        BigDecimal sum = BigDecimal.ZERO;
        for (Carts carts : exiCarts) {
            ProdAttrPrice condPrice = new ProdAttrPrice();
            condPrice.setProdAttrId(carts.getProdAttrId());
            condPrice.setProdId(carts.getProdId());
            condPrice.setBuyerRoleKey(ERoleKey.CUSTOMER.getCode());

            List<ProdAttrPrice> priceList = prodAttrPriceMapper.selectProdAttrPriceList(condPrice);

            if (StringUtils.isNotEmpty(priceList)) {
                sum = sum.add(new BigDecimal(carts.getQuantity()).multiply(priceList.get(0).getPrice()));
            }

            CartsResponse cartsResponse = new CartsResponse();

            cartsResponse.setId(carts.getId());
            cartsResponse.setUserId(userId);
            cartsResponse.setProdId(carts.getProdId());
            cartsResponse.setProdAttrId(carts.getProdAttrId());
            cartsResponse.setQuantity(carts.getQuantity());
            cartsResponse.setCheckFlag(carts.getCheckFlag());

            ProdPic condPic = new ProdPic();
            condPic.setProdId(carts.getProdId());
            condPic.setDelFlag(EDelFlag.NO.getVal());

            List<ProdPic> picList = prodPicServiceImpl.selectProdPicList(condPic);
            cartsResponse.setProdPicList(picList);

        }

        return new CartsListResponse(cartsResponseList, sum.toPlainString());
    }
}
