package com.heyu.api.controller.vv;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.order.VvActivityDTO;
import com.heyu.api.alibaba.request.mm.order.resp.PropertyNameValue;
import com.heyu.api.alibaba.request.vv.AppShippingCartDeleteRequest;
import com.heyu.api.alibaba.request.vv.AppShippingCartRequest;
import com.heyu.api.alibaba.request.vv.AppShoppingCartAddRequest;
import com.heyu.api.common.annotation.AppRealyLogin;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.dao.vv.*;
import com.heyu.api.data.dto.vv.VvShoppingCartDTO;
import com.heyu.api.data.entity.vv.*;
import com.heyu.api.data.utils.*;
import com.heyu.api.utils.ISelect;
import com.heyu.api.utils.PPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


@Slf4j
@RestController
@RequestMapping("/app/shipping/cart")
@AppRealyLogin
public class AppShoppingCartController {


    @Autowired
    private VvShoppingCartDao vvShoppingCartDao;

    @Autowired
    private VvBuyerDao vvBuyerDao;

    @Autowired
    private VvProductDao vvProductDao;


    @Autowired
    private VvSkuPropertyValueDao vvSkuPropertyValueDao;

    @Autowired
    private VvSkuDao vvSkuDao;

    @Autowired
    private RedisUtils redisUtils;


    @Autowired
    private VvActivityDao vvActivityDao;
    /***
     * 列表   /app/shipping/cart/list
     */
    @Describe("购物车列表")
    @RequestMapping("/list")
    @AppRealyLogin
    public R list(@RequestBody AppShippingCartRequest appShippingCartRequest) {
        PPageUtils pageUtils = PPageUtils.startPage(appShippingCartRequest.getPageNum(), appShippingCartRequest.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvShoppingCartDao.selectAppByCondition(page,
                                appShippingCartRequest.getProductName(),
                                appShippingCartRequest.getBuyerId()
                        );
                    }
                });
        List<VvShoppingCartEntity> shoppingCartEntities = pageUtils.getRows();
        List<VvShoppingCartDTO> vvShoppingCartDTOS = new ArrayList<>();
        List<Long> skuIds = new ArrayList<>();
        for (VvShoppingCartEntity vvShoppingCartEntity : shoppingCartEntities) {
            skuIds.add(vvShoppingCartEntity.getSkuId());
        }

        if(CollectionUtils.isNotEmpty(skuIds)){
            List<VvSkuEntity> vvSkuEntities = vvSkuDao.selectVvSkuListByIds(skuIds);
            for (VvShoppingCartEntity vvShoppingCartEntity : shoppingCartEntities) {
                VvShoppingCartDTO vvShoppingCartDTO = new VvShoppingCartDTO();
                BeanUtils.copyProperties(vvShoppingCartEntity, vvShoppingCartDTO);

                for (VvSkuEntity vvSkuEntity : vvSkuEntities) {
                    if (NumberUtil.equals(vvShoppingCartEntity.getSkuId(), vvSkuEntity.getId())) {
                        vvShoppingCartDTO.setVvSkuEntity(vvSkuEntity);
                    }
                }
                vvShoppingCartDTOS.add(vvShoppingCartDTO);
            }
        }
        pageUtils.setRows(vvShoppingCartDTOS);
        return R.ok().setData(pageUtils);
    }


    /***
     * 更新 或 添加  ，如数量更新            /app/shipping/cart/addOrUpdate
     */
    @Describe("加购物车")
    @RequestMapping("/addOrUpdate")
    @AppRealyLogin
    public R add(@RequestBody AppShoppingCartAddRequest vvShoppingCartAddRequest) {
        VvBuyerEntity vvBuyerEntity = vvBuyerDao.selectVvBuyerById(vvShoppingCartAddRequest.getBuyerId());
        String activityTokenInfo = redisUtils.get(ApiConstants.token_activity + vvShoppingCartAddRequest.getToken());

        VvActivityDTO vvActivityDTO = null;
        if(StringUtils.isNotEmpty(activityTokenInfo)){
            vvActivityDTO = JSON.parseObject(activityTokenInfo, VvActivityDTO.class);
        }
        VvShoppingCartEntity vvShoppingCartEntity = new VvShoppingCartEntity();
        vvShoppingCartEntity.setNum(vvShoppingCartAddRequest.getNum());
        vvShoppingCartEntity.setBuyerName(vvBuyerEntity.getBuyerName());
        vvShoppingCartEntity.setBuyerPhone(vvBuyerEntity.getBuyerPhone());
        vvShoppingCartEntity.setBuyerWeixin(vvBuyerEntity.getBuyerWeixin());
        vvShoppingCartEntity.setSkuId(vvShoppingCartAddRequest.getSkuId());
        vvShoppingCartEntity.setBuyerId(vvShoppingCartAddRequest.getBuyerId());

        VvSkuEntity vvSkuEntity = vvSkuDao.selectVvSkuById(vvShoppingCartAddRequest.getSkuId());
        vvShoppingCartEntity.setProductId(vvSkuEntity.getProductId());
        vvShoppingCartEntity.setSinglePrice(vvSkuEntity.getPromotionPrice());
        vvShoppingCartEntity.setAllPrice(BigDecimalUtil.multiply(vvSkuEntity.getPromotionPrice(), new BigDecimal(vvShoppingCartAddRequest.getNum())));

        if (vvActivityDTO != null) {
            VvActivityEntity vvActivityEntity = vvActivityDao.selectVvActivityById(vvActivityDTO.getActivityId());
            if (vvActivityEntity != null) {
                vvShoppingCartEntity.setActivityInfo(JSON.toJSONString(vvActivityDTO));
                vvShoppingCartEntity.setAllDiscountAmount(BigDecimalUtil.multiply(vvActivityEntity.getDiscountAmount(), new BigDecimal(vvShoppingCartAddRequest.getNum())));
                vvShoppingCartEntity.setSingleDiscountAmount(vvActivityEntity.getDiscountAmount());
            }
        }
        List<VvSkuPropertyValueEntity> vvSkuPropertyValueEntities = vvSkuPropertyValueDao.selectVvSkuPropertyValueBySkuId(vvShoppingCartEntity.getSkuId());

        VvSkuPropertyValueEntity vvSkuPropertyValue = vvSkuPropertyValueEntities.get(0);
        VvProductEntity vvProduct = vvProductDao.selectVvProductById(vvSkuPropertyValue.getProductId());
        vvShoppingCartEntity.setProductName(vvProduct.getTitle());

        String imageUrl =vvSkuEntity.getImageUrl();
        if(StringUtils.isBlank(imageUrl)){
            imageUrl = vvProduct.getMainImageUrl();
        }

        vvShoppingCartEntity.setImageUrl(imageUrl);
        List<PropertyNameValue> propertyNameValues = new ArrayList<>();
        for (VvSkuPropertyValueEntity vvSkuPropertyValueEntity : vvSkuPropertyValueEntities) {
            PropertyNameValue propertyNameValue = new PropertyNameValue();
            propertyNameValue.setPropertyName(vvSkuPropertyValueEntity.getProductPropertyName());
            propertyNameValue.setPropertyValue(vvSkuPropertyValueEntity.getProductPropertyValue());
            propertyNameValues.add(propertyNameValue);
        }
        vvShoppingCartEntity.setSkuInfo(JSON.toJSONString(propertyNameValues));
        vvShoppingCartDao.insertOrUpdateVvShoppingCart(vvShoppingCartEntity);
        return R.ok();
    }

    /***
     * /app/shipping/cart/delete
     */
    @Describe("删除购物车中商品")
    @RequestMapping("/delete")
    @AppRealyLogin
    public R delete(@RequestBody AppShippingCartDeleteRequest appShippingCartDeleteRequest) {
        vvShoppingCartDao.deleteVvShoppingCartByIds(appShippingCartDeleteRequest.getShoppingCartIds());
        return R.ok();
    }


}
