package com.starry.admin.modules.weichat.controller;

import cn.hutool.core.util.StrUtil;
import com.starry.admin.common.aspect.CustomUserLogin;
import com.starry.admin.common.conf.ThreadLocalRequestDetail;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.custom.module.entity.PlayCustomUserInfoEntity;
import com.starry.admin.modules.play.module.vo.PlayCommodityInfoVo;
import com.starry.admin.modules.play.service.IPlayCommodityInfoService;
import com.starry.admin.modules.shop.module.entity.PlayCouponInfoEntity;
import com.starry.admin.modules.shop.module.vo.PlayCouponDetailsReturnVo;
import com.starry.admin.modules.shop.service.IPlayCouponDetailsService;
import com.starry.admin.modules.shop.service.IPlayCouponInfoService;
import com.starry.admin.modules.weichat.entity.WxCouponOrderQueryVo;
import com.starry.admin.modules.weichat.entity.WxCouponOrderReturnVo;
import com.starry.admin.modules.weichat.entity.WxCouponReceiveReturnVo;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 * @since 2024/7/4 11:33
 **/
@Slf4j
@RestController
@RequestMapping("/wx/coupon/")
public class WxCouponController {


    @Resource
    IPlayCouponDetailsService couponDetailsService;

    @Resource
    IPlayCouponInfoService couponInfoService;

    @Resource
    private IPlayCommodityInfoService playCommodityInfoService;


    @CustomUserLogin
    @GetMapping("/custom/obtainCoupon")
    public R queryAll(@RequestParam("id") String id) {
        PlayCustomUserInfoEntity customUserInfo = ThreadLocalRequestDetail.getCustomUserInfo();
        if (StrUtil.isEmpty(id)) {
            throw new CustomException("请求参数异常,优惠券ID不能为");
        }
        PlayCouponInfoEntity entity = couponInfoService.selectPlayCouponInfoById(id);
        String msg = couponInfoService.getReasonForNotObtainingCoupons(entity, customUserInfo);
        boolean success = StrUtil.isBlank(msg);
        // 优惠券领取验证通过，发放优惠券
        if (success) {
            couponDetailsService.create(customUserInfo.getId(), customUserInfo.getNickname(), customUserInfo.getLevelId(), entity.getId(), "1", "1");
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        result.put("msg", msg);
        return R.ok(result);
    }


    /**
     * 查询所有优惠券，以及本人是否可领取
     *
     * @return com.starry.common.result.R
     * @author admin
     * @since 2024/7/8 13:53
     **/
    @CustomUserLogin
    @PostMapping("/custom/queryAll")
    public R queryAll() {
        List<PlayCouponDetailsReturnVo> obtainedCoupons = couponDetailsService.selectByCustomId(ThreadLocalRequestDetail.getCustomUserInfo().getId());
        List<PlayCouponInfoEntity> couponInfoEntities = couponInfoService.queryAll();
        List<WxCouponReceiveReturnVo> returnVos = new ArrayList<>(couponInfoEntities.size());
        for (PlayCouponInfoEntity couponInfoEntity : couponInfoEntities) {
            if ("0".equals(couponInfoEntity.getCouponOnLineState())) {
                continue;
            }
            WxCouponReceiveReturnVo vo = ConvertUtil.entityToVo(couponInfoEntity, WxCouponReceiveReturnVo.class);
            for (PlayCouponDetailsReturnVo obtainedCoupon : obtainedCoupons) {
                if (obtainedCoupon.getCouponId().equals(couponInfoEntity.getId())) {
                    vo.setObtained("1");
                }
            }
            returnVos.add(vo);
        }
        return R.ok(returnVos);
    }


    /**
     * 顾客查询订单可使用优惠券
     */
    @CustomUserLogin
    @PostMapping("/custom/queryByOrder")
    public R queryByOrder(@Validated @RequestBody WxCouponOrderQueryVo vo) {
        List<PlayCouponDetailsReturnVo> list = couponDetailsService.selectByCustomId(ThreadLocalRequestDetail.getCustomUserInfo().getId());
        PlayCommodityInfoVo commodityInfo = playCommodityInfoService.queryCommodityInfo(vo.getCommodityId());
        // 优惠券列表
        List<WxCouponOrderReturnVo> couponReturnVos = new ArrayList<>();
        for (PlayCouponDetailsReturnVo couponDetails : list) {
            PlayCouponInfoEntity couponInfo = couponInfoService.selectPlayCouponInfoById(couponDetails.getCouponId());
            WxCouponOrderReturnVo wxCouponReturnVo = ConvertUtil.entityToVo(couponDetails, WxCouponOrderReturnVo.class);
            String couponReasonForUnavailableUse = couponInfoService.getCouponReasonForUnavailableUse(couponInfo, vo.getPlaceType(), vo.getCommodityId(), vo.getCommodityQuantity(), commodityInfo.getCommodityPrice());
            if (StrUtil.isEmpty(couponReasonForUnavailableUse)) {
                wxCouponReturnVo.setAvailable("1");
            } else {
                wxCouponReturnVo.setAvailable("0");
                wxCouponReturnVo.setReasonForUnavailableUse(couponReasonForUnavailableUse);
            }
            couponReturnVos.add(wxCouponReturnVo);
        }
        return R.ok(couponReturnVos);
    }



}
