package com.kmall.api.api;

import com.kmall.api.annotation.LoginUser;
import com.kmall.api.dto.CampMinusDto;
import com.kmall.api.dto.CheckOutDto;
import com.kmall.api.dto.UserCouponDto;
import com.kmall.api.entity.CartVo;
import com.kmall.api.entity.UserCouponVo;
import com.kmall.api.entity.UserVo;
import com.kmall.api.entity.mk.MkStoreCampMinusVo;
import com.kmall.api.entity.mk.MkStoreTicketDiscountVo;
import com.kmall.api.service.ApiCartService;
import com.kmall.api.service.ApiUserCouponService;
import com.kmall.api.service.mk.ApiMkStoreCampMinusService;
import com.kmall.api.service.mk.ApiMkStoreTicketDiscountService;
import com.kmall.api.service.mk.ApiMkStoreTopicStatService;
import com.kmall.api.util.ApiBaseAction;
import com.kmall.common.constant.Dict;
import com.kmall.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 门店满减满折活动Controller
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2019-05-07 16:06:28
 */
@RestController
@RequestMapping("/api/campminus")
public class ApiStoreCampMinusController extends ApiBaseAction {
    @Autowired
    private ApiMkStoreCampMinusService apiMkStoreCampMinusService;
    @Autowired
    private ApiUserCouponService apiUserCouponService;
    @Autowired
    private ApiCartService apiCartService;
    @Autowired
    private ApiMkStoreTopicStatService apiMkStoreTopicStatService;

    /**
     * 查看信息
     */
    @GetMapping("detail")
    public Object info(Long storeId, Integer campMinusId,@LoginUser UserVo loginUser) {
        MkStoreCampMinusVo mkStoreCampMinusVo = null;
        if(campMinusId != null){
            mkStoreCampMinusVo = apiMkStoreCampMinusService.queryObject(campMinusId);
        }
        if(mkStoreCampMinusVo != null){
            String startDate = DateUtils.format(mkStoreCampMinusVo.getCampBegTime(), DateUtils.PATTERN_YYYY_MM_DD_HH_MM);
            String endDate = DateUtils.format(mkStoreCampMinusVo.getCampEndTime(), DateUtils.PATTERN_YYYY_MM_DD_HH_MM);
            String inValidDate = startDate + " 至 " + endDate;
            mkStoreCampMinusVo.setInValidTime(inValidDate);

            Integer storeTopicId = mkStoreCampMinusVo.getStoreTopicId();
            //记录活动营销统计访客量数据
            apiMkStoreTopicStatService.saveStoreTopicStatByVisitData(storeTopicId, storeId, getUserId());
        }
        return toResponsSuccess(mkStoreCampMinusVo);
    }

    /**
     * 详情页查询该商品可支持的满减满折信息
     */
    @GetMapping("getStoreCampMinusByGoodsIdList")
    public Object getStoreCampMinusByGoodsIdList(@LoginUser UserVo loginUser, Integer storeId, Integer goodsId,
                                           @RequestParam(value = "page", defaultValue = "1") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
        List<MkStoreCampMinusVo> campMinusVoList = apiMkStoreCampMinusService.queryStoreCampMinusByGoodsId(storeId, null, goodsId);
        List<MkStoreCampMinusVo> resultCampMinusList = new ArrayList<>();
        if(size <= campMinusVoList.size()) {
            for (int i = 0; i < size; i++) {
                MkStoreCampMinusVo campMinusVo = campMinusVoList.get(i);
                resultCampMinusList.add(campMinusVo);
            }
        }
        if(size > campMinusVoList.size()) {
            for (MkStoreCampMinusVo campMinusVo: campMinusVoList) {
                resultCampMinusList.add(campMinusVo);
            }
        }
        return toResponsSuccess(resultCampMinusList);
    }

    /**
     * 确认订单页，查看订单商品所支持的满减满折列表
     */
    @GetMapping("getCampminusByCheckCartList")
    public Object getCampminusByCheckCartList(@LoginUser UserVo loginUser, Integer storeId, String checkCart, String bizType, Long goodsId, Integer number) {
        CheckOutDto checkOutDto = new CheckOutDto();
        checkOutDto.setCheckCart(checkCart);
        //商品id不为空，则提交订单是从商品详情页立即购买过来的
        if(goodsId != null){
            checkOutDto.setGoodsId(goodsId);
            checkOutDto.setNumber(number);
        }
        checkOutDto.setLoginUser(loginUser);
        //获取要购买的商品
        Map<String, Object> cartData = apiCartService.getCartMoney(loginUser, checkOutDto, Long.valueOf(storeId));

        List<CartVo> checkedGoodsList = new ArrayList();
        List<CartVo> cartGoodsList = (List<CartVo>) cartData.get("cartList");
        for (CartVo cartEntity : cartGoodsList) {
            if (cartEntity.getChecked() == 1) {
                if(bizType.equalsIgnoreCase(cartEntity.getGoodsBizType())) {
                    checkedGoodsList.add(cartEntity);
                }
            }
        }
        //计算订单的费用 商品总价
        BigDecimal goodsTotalPrice = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount");
        BigDecimal goodsTotalPrice00 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount00");
        BigDecimal goodsTotalPrice02 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount02");
        BigDecimal goodsTotalPrice10 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount10");
        BigDecimal goodsTotalPrice11 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount11");
        if(bizType.equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
            goodsTotalPrice = goodsTotalPrice00;
        }else if(bizType.equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
            goodsTotalPrice = goodsTotalPrice02;
        }else if(bizType.equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
            goodsTotalPrice = goodsTotalPrice10;
        }else if(bizType.equalsIgnoreCase(Dict.orderBizType.item_11.getItem())){
            goodsTotalPrice = goodsTotalPrice11;
        }
        //获取可用的优惠券信息 抵用券
        UserCouponDto userCouponDto = new UserCouponDto();
        userCouponDto.setUserId(loginUser.getId());
        userCouponDto.setStoreId(getStoreId());
        userCouponDto.setGoodsTotalPrice(goodsTotalPrice);
        userCouponDto.setCartGoodsList(checkedGoodsList);
        userCouponDto.setBizType(bizType);
        List<CampMinusDto> enableCouponList = apiMkStoreCampMinusService.matchUserCampMinusList(userCouponDto);
        return toResponsSuccess(enableCouponList);
    }
}
