package com.yiqi.api.controller.activity;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.activity.domain.ActivityApplyOrg;
import com.yiqi.activity.domain.ActivityClothItem;
import com.yiqi.activity.domain.vo.ActivityClothItemVO;
import com.yiqi.activity.domain.vo.ActivityDiscountInfoQueryVO;
import com.yiqi.activity.domain.vo.ActivityDiscountInfoVO;
import com.yiqi.activity.service.*;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.core.domain.vo.SelectItemVO;
import com.yiqi.common.enums.*;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.activity.domain.ActivityCouponInfo;
import com.yiqi.activity.domain.ActivityInfo;
import com.yiqi.activity.domain.dto.weapp.WeAppActivityCouponQueryDTO;
import com.yiqi.activity.domain.dto.weapp.WeAppActivityDiscountQueryDTO;
import com.yiqi.activity.domain.vo.ActivityCouponItemVO;
import com.yiqi.activity.domain.vo.weapp.WeAppActivityCouponInfoVO;
import com.yiqi.activity.domain.vo.weapp.WeAppActivityCouponQueryVO;
import com.yiqi.activity.domain.vo.weapp.WeAppActivityDiscountQueryVO;
import com.yiqi.app.domain.AppUser;
import com.yiqi.app.service.IAppUserService;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AuthHolder;
import com.yiqi.common.core.domain.R;
import com.yiqi.order.domain.OrderActivity;
import com.yiqi.order.service.IOrderActivityService;
import com.yiqi.system.domain.SysStore;
import com.yiqi.system.service.ISysStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "app全场折扣活动")
@RestController
@RequestMapping(value = UrlConstants.wxappApi + "/activity/discount")
public class WeAppActivityDiscountController extends BaseController {

    @Autowired
    private IActivityInfoService activityInfoService;
    @Autowired
    private IActivityDiscountInfoService activityDiscountInfoService;
    @Autowired
    private ISysStoreService sysStoreService;
    @Autowired
    private IActivityClothItemService activityClothItemService;
    @Autowired
    private IActivityApplyOrgService activityApplyOrgService;
    @Autowired
    private IAppUserService appUserService;
    /**
     * 查询全场折扣活动列表
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "查询全场折扣活动列表")
    public R<List<WeAppActivityDiscountQueryVO>> findPage(@RequestBody WeAppActivityDiscountQueryDTO weAppActivityDiscountQueryDTO) {
        List<WeAppActivityDiscountQueryVO> list = activityInfoService.selectActivityDiscountList(weAppActivityDiscountQueryDTO);
        return R.ok(list);
    }

    /**
     * 获取全场折扣活动详细信息
     */
    @GetMapping(value = "getInfo")
    @ApiOperation(value = "获取全场折扣活动详细信息")
    public AjaxResult getInfo(@RequestParam(value = "id", required = false) Long id) {
        if (id == null) {
            ActivityDiscountInfoVO activityDiscountInfoVO = new ActivityDiscountInfoVO();
            activityDiscountInfoVO.setStoreVOS(sysStoreService.list(
                            new QueryWrapper<SysStore>().lambda()
                                    .eq(SysStore::getDelFlag, DeleteStatus.OK.getCode())
                                    .eq(SysStore::getStatus, StatusType.OK.getCode())).stream()
                    .map(SelectItemVO::new).collect(Collectors.toList()));
            return success(activityDiscountInfoVO);
        }
        ActivityInfo activityInfo = activityInfoService.selectActivityInfoById(id);
        if (activityInfo == null || !ActivityType.all_discount.getCode().equals(activityInfo.getActivityType())) {
            throw new ServiceException("未查询到活动");
        }
        ActivityDiscountInfoVO activityDiscountInfoVO = new ActivityDiscountInfoVO();
        BeanUtils.copyProperties(activityInfo, activityDiscountInfoVO);
        activityDiscountInfoVO.setStoreVOS(sysStoreService.list(
                        new QueryWrapper<SysStore>().lambda()
                                .eq(SysStore::getDelFlag, DeleteStatus.OK.getCode())
                                .eq(SysStore::getStatus, StatusType.OK.getCode())).stream()
                .map(SelectItemVO::new).collect(Collectors.toList()));
        activityDiscountInfoVO.setSelectStoreIds(activityApplyOrgService.list(new QueryWrapper<ActivityApplyOrg>()
                        .lambda().eq(ActivityApplyOrg::getActivityId, id))
                .stream().filter(item -> SourceType.STORE.getCode().equals(item.getSourceType()))
                .map(ActivityApplyOrg::getOrgId).collect(Collectors.toList()));
        // 为了编辑的时候，直接按id去更新活动信息
        activityDiscountInfoVO.setId(activityInfo.getId());
        return success(activityDiscountInfoVO);
    }

    /**
     * 获取门店折扣活动列表
     */
    @GetMapping(value = "getDiscountByStoreId")
    @ApiOperation(value = "获取门店折扣活动列表")
    public R getDiscountListByStoreId(Long storeId) {
        List<ActivityDiscountInfoQueryVO> list = activityDiscountInfoService.getDiscountListByStoreId(storeId);
        for (ActivityDiscountInfoQueryVO activityDiscountInfoQueryVO : list) {
            if (activityDiscountInfoQueryVO.getUserScope().equals(UserScopeType.NEW.getCode())) {
                if (!appUserService.checkUserIsNewUser(AuthHolder.userId())) {
                    continue;
                }
            }
            if (activityDiscountInfoQueryVO.getUserScope().equals(UserScopeType.MEMBER.getCode())) {
                if (!appUserService.checkUserIsMember(AuthHolder.userId())) {
                    continue;
                }
            }
            //指定衣服
            if (activityDiscountInfoQueryVO.getClothScope().equals("1")) {
                ActivityClothItem activityClothItem = new ActivityClothItem();
                activityClothItem.setActivityId(activityDiscountInfoQueryVO.getId());
                List<ActivityClothItemVO> activityClothItemVOList = activityClothItemService.selectActivityClothItemList(activityClothItem);
                activityDiscountInfoQueryVO.setActivityClothItemVOList(activityClothItemVOList);
            }
        }
        return R.ok(list);
    }
}









