package com.heyu.api.controller.vv;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.VvProductDetailRequest;
import com.heyu.api.alibaba.request.mm.order.VvActivityDTO;
import com.heyu.api.alibaba.request.vv.AppIndexPageRequest;
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.VvProductFrontDTO;
import com.heyu.api.data.dto.vv.VvProductDTO;
import com.heyu.api.data.dto.vv.VvProductPropertyDTO;
import com.heyu.api.data.dto.vv.VvSkuDTO;
import com.heyu.api.data.entity.vv.*;
import com.heyu.api.data.enums.ProductStatusEnums;
import com.heyu.api.data.utils.NumberUtil;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.RedisUtils;
import com.heyu.api.data.utils.StringUtils;
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.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/app/index")
public class AppIndexPageController {

    @Autowired
    private VvProductDao vvProductDao;


    @Autowired
    private VvProductDetailDao vvProductDetailDao;


    @Autowired
    private VvSkuDao vvSkuDao;


    @Autowired
    private VvProductPropertyDao vvProductPropertyDao;


    @Autowired
    private VvSkuPropertyValueDao vvSkuPropertyValueDao;

    @Autowired
    private VvProductPropertyValueDao vvProductPropertyValueDao;


    @Autowired
    private VvBuyerDao vvBuyerDao;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private VvActivityDao vvActivityDao;

    /***
     * 列表        /app/index/page/list
     */
    @RequestMapping("/page/list")
    @Describe("商品首页列表")
    public R list(@RequestBody AppIndexPageRequest vvProductRequest) {
        vvProductRequest.setStatus(ProductStatusEnums.online.getStatus());
        if (NumberUtil.equals(vvProductRequest.getIsTest(), 1)) {
            Long buyerId = vvProductRequest.getBuyerId();
            VvBuyerEntity vvBuyerEntity = vvBuyerDao.selectVvBuyerIsTestById(buyerId, 1);
            if (vvBuyerEntity == null) {
                return R.error("非内部用户");
            }
        }
        PPageUtils pageUtils = PPageUtils.startPage(vvProductRequest.getPageNum(), vvProductRequest.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvProductDao.selectIndexVvProduct(page,
                                vvProductRequest.getProductName(),
                                vvProductRequest.getFrontPage(),
                                vvProductRequest.getStatus(),
                                vvProductRequest.getCategoryId1(),
                                vvProductRequest.getCategoryId2(),
                                vvProductRequest.getCategoryId3(),
                                vvProductRequest.getIsTest(),
                                vvProductRequest.getIsNew(),
                                vvProductRequest.getIsFlash(),
                                vvProductRequest.getSalePriceSort(),
                                vvProductRequest.getSaleCountSort(),
                                vvProductRequest.getCreateTimestampSort()
                        );
                    }
                });
        // 折扣价格
        String activityTokenInfo = redisUtils.get(ApiConstants.token_activity + vvProductRequest.getToken());
        VvActivityEntity vvActivityEntity = null;
        VvActivityDTO vvActivityDTO = null;
        if (StringUtils.isNotEmpty(activityTokenInfo)) {
            vvActivityDTO = JSONObject.parseObject(activityTokenInfo, VvActivityDTO.class);
            vvActivityEntity = vvActivityDao.selectVvActivityById(vvActivityDTO.getActivityId());
        }

        List<VvProductEntity> vvProductEntities = pageUtils.getRows();
        for (VvProductEntity vvProductEntity : vvProductEntities) {
            VvProductFrontDTO vvProductFrontDTO = new VvProductFrontDTO();
            BeanUtils.copyProperties(vvProductEntity, vvProductFrontDTO);
            if (vvActivityEntity != null) {
                vvProductFrontDTO.setDiscountAmount(vvActivityEntity.getDiscountAmount());
            }
        }
        pageUtils.setRows(vvProductEntities);
        return R.ok().setData(pageUtils);
    }


    // /app/index/product/detail
    @Describe("商品详情页")
    @RequestMapping("/product/detail")
    public R detail(@RequestBody VvProductDetailRequest vvProductDetailRequest) {
        VvProductDTO vvProductDTO = new VvProductDTO();
        VvProductEntity vvProduct = vvProductDao.selectVvProductById(vvProductDetailRequest.getProductId());

        BeanUtils.copyProperties(vvProduct, vvProductDTO);
        List<VvProductDetailEntity> productDetailEntities = vvProductDetailDao.selectVvProductDetailByProductId(vvProductDetailRequest.getProductId());
        vvProductDTO.setVvProductDetailList(productDetailEntities);
        List<VvSkuEntity> vvSkuEntities = vvSkuDao.selectAppSkuByProductId(vvProductDetailRequest.getProductId());
        List<VvSkuDTO> vvSkuDTOS = new ArrayList<>();
        List<VvSkuPropertyValueEntity> vvSkuPropertyValueList = vvSkuPropertyValueDao.selectVvSkuPropertyValueByProductId(vvProduct.getId());

        Map<Long, List<VvSkuPropertyValueEntity>> vvSkuPropertyValueMap = new LinkedHashMap<>();

        for (VvSkuPropertyValueEntity vvSkuPropertyValueEntity : vvSkuPropertyValueList) {
            List<VvSkuPropertyValueEntity> vvSkuPropertyValueEntities = vvSkuPropertyValueMap.get(vvSkuPropertyValueEntity.getSkuId());
            if (vvSkuPropertyValueEntities == null) {
                vvSkuPropertyValueEntities = new ArrayList<>();
            }
            vvSkuPropertyValueEntities.add(vvSkuPropertyValueEntity);
            vvSkuPropertyValueMap.put(vvSkuPropertyValueEntity.getSkuId(), vvSkuPropertyValueEntities);
        }
        String activityTokenInfo = redisUtils.get(ApiConstants.token_activity + vvProductDetailRequest.getToken());

        // 折扣价格
        VvActivityDTO vvActivityDTO = null;
        // 折扣价格
        VvActivityEntity vvActivityEntity = null;
        if (StringUtils.isNotEmpty(activityTokenInfo)) {
            vvActivityDTO = JSONObject.parseObject(activityTokenInfo, VvActivityDTO.class);
            vvActivityEntity = vvActivityDao.selectVvActivityById(vvActivityDTO.getActivityId());
        }

        if(StringUtils.isNotEmpty(activityTokenInfo)){
            vvActivityDTO = JSONObject.parseObject(activityTokenInfo, VvActivityDTO.class);
        }

        for (VvSkuEntity vvSkuEntity : vvSkuEntities) {
            VvSkuDTO vvSkuDTO = new VvSkuDTO();
            BeanUtils.copyProperties(vvSkuEntity, vvSkuDTO);
            List<VvSkuPropertyValueEntity> vvSkuPropertyValueEntities = vvSkuPropertyValueMap.get(vvSkuEntity.getId());
            vvSkuDTO.setVvSkuPropertyValueList(vvSkuPropertyValueEntities);
            if(vvActivityDTO !=null){
                vvSkuDTO.setDiscountAmount(vvActivityEntity.getDiscountAmount());
            }
            vvSkuDTOS.add(vvSkuDTO);
        }
        vvProductDTO.setVvSkuList(vvSkuDTOS);
        List<VvProductPropertyEntity> vvProductPropertyEntities = vvProductPropertyDao.selectVvProductPropertyByProductId(vvProductDetailRequest.getProductId());
        List<VvProductPropertyValueEntity> productPropertyValueList = vvProductPropertyValueDao.selectVvProductPropertyValueByProductIdProductId(vvProduct.getId());
        Map<Long, List<VvProductPropertyValueEntity>> productPropertyValueMap = new LinkedHashMap<>();
        for (VvProductPropertyValueEntity productPropertyValueEntity : productPropertyValueList) {
            List<VvProductPropertyValueEntity> vvProductPropertyValueEntities = productPropertyValueMap.get(productPropertyValueEntity.getProductPropertyId());
            if (vvProductPropertyValueEntities == null) {
                vvProductPropertyValueEntities = new ArrayList<>();
            }
            vvProductPropertyValueEntities.add(productPropertyValueEntity);
            productPropertyValueMap.put(productPropertyValueEntity.getProductPropertyId(), vvProductPropertyValueEntities);
        }

        List<VvProductPropertyDTO> vvProductPropertyDTOS = new ArrayList<>();
        for (VvProductPropertyEntity vvProductPropertyEntity : vvProductPropertyEntities) {

            VvProductPropertyDTO vvProductPropertyDTO = new VvProductPropertyDTO();

            BeanUtils.copyProperties(vvProductPropertyEntity, vvProductPropertyDTO);

            List<VvProductPropertyValueEntity> productPropertyValues = productPropertyValueMap.get(vvProductPropertyEntity.getId());

            vvProductPropertyDTO.setVvProductPropertyValueList(productPropertyValues);
            vvProductPropertyDTOS.add(vvProductPropertyDTO);
        }
        vvProductDTO.setVvProductPropertyList(vvProductPropertyDTOS);
        return R.ok().setData(vvProductDTO);
    }


}
