package com.yiqi.activity.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.activity.domain.ActivityApplyOrg;
import com.yiqi.activity.domain.ActivityCouponItem;
import com.yiqi.activity.domain.ActivityInfo;
import com.yiqi.activity.domain.dto.ActivityAppCouponItemQueryDTO;
import com.yiqi.activity.domain.dto.ActivityCouponInfoDTO;
import com.yiqi.activity.domain.vo.ActivityAppCouponItemVO;
import com.yiqi.activity.domain.vo.ActivityCouponInfoVO;
import com.yiqi.activity.service.IActivityApplyOrgService;
import com.yiqi.activity.service.IActivityCouponItemService;
import com.yiqi.activity.service.IActivityInfoService;
import com.yiqi.app.domain.AppCouponType;
import com.yiqi.common.enums.ActivityType;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.activity.mapper.ActivityCouponInfoMapper;
import com.yiqi.activity.domain.ActivityCouponInfo;
import com.yiqi.activity.service.IActivityCouponInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import static com.yiqi.common.utils.SecurityUtils.getUserId;
import static com.yiqi.common.utils.SecurityUtils.getUsername;

/**
 * 注册送券活动Service业务层处理
 *
 * @author xiaohei
 * @date 2023-09-08
 */
@Service
public class ActivityCouponInfoServiceImpl extends ServiceImpl<ActivityCouponInfoMapper, ActivityCouponInfo> implements IActivityCouponInfoService {

    @Autowired
    private IActivityInfoService activityInfoService;
    @Autowired
    private IActivityApplyOrgService activityApplyOrgService;
    @Autowired
    private IActivityCouponItemService activityCouponItemService;

    /**
     * 查询注册送券活动
     *
     * @param id 注册送券活动主键
     * @return 注册送券活动
     */
    @Override
    public ActivityCouponInfoVO selectActivityCouponInfoById(Long id) {
        return baseMapper.selectActivityCouponInfoById(id);
    }

    @Override
    public ActivityCouponInfoVO selectActivityCouponInfoBySendCoupon(Long activityId, Long couponTypeId) {
        return baseMapper.selectActivityCouponInfoBySendCoupon(activityId, couponTypeId);
    }

    /**
     * 查询注册送券活动列表
     *
     * @param activityCouponInfoDTO 注册送券活动
     * @return 注册送券活动
     */
    @Override
    public List<ActivityCouponInfoVO> selectActivityCouponInfoList(ActivityCouponInfoDTO activityCouponInfoDTO) {
        List<ActivityCouponInfoVO> couponInfoVOList = baseMapper.selectActivityCouponInfoList(activityCouponInfoDTO);
        for (ActivityCouponInfoVO activityCouponInfoVO : couponInfoVOList) {
            long count = activityCouponItemService.count(new QueryWrapper<ActivityCouponItem>().eq("activity_id",activityCouponInfoVO.getId()).
                    eq("del_flag", DeleteStatus.OK.getCode()));
            activityCouponInfoVO.setCouponNum(count);
        }
        return couponInfoVOList;
    }

    /**
     * 新增注册送券活动
     *
     * @param activityCouponInfoDTO 注册送券活动
     * @return 结果
     */
    @Override
    public int insertActivityCouponInfo(ActivityCouponInfoDTO activityCouponInfoDTO) {
        activityCouponInfoDTO.setCreateTime(DateUtils.getNowDate());
        ActivityInfo activityInfo = new ActivityInfo();
        BeanUtils.copyProperties(activityCouponInfoDTO, activityInfo);
        activityInfo.setActivityType(ActivityType.regist.getCode());
        int row = activityInfoService.insertActivityInfo(activityInfo);
        ActivityCouponInfo activityCouponInfo = new ActivityCouponInfo();
        BeanUtils.copyProperties(activityCouponInfoDTO, activityCouponInfo);
        activityCouponInfo.setActivityId(activityInfo.getId());
        baseMapper.insertActivityCouponInfo(activityCouponInfo);

        List<ActivityApplyOrg> activityApplyOrgs = new ArrayList<>();
        for (Long selectStoreId : activityCouponInfoDTO.getSelectStoreIds()) {
            ActivityApplyOrg activityApplyOrg = new ActivityApplyOrg();
            activityApplyOrg.setActivityId(activityInfo.getId());
            activityApplyOrg.setOrgId(selectStoreId);
            activityApplyOrg.setSourceType(SourceType.STORE.getCode());
            activityApplyOrg.setCreateBy(getUsername());
            activityApplyOrg.setCreateById(getUserId());
            activityApplyOrg.setCreateTime(DateUtils.getNowDate());
            activityApplyOrgs.add(activityApplyOrg);
        }
        activityApplyOrgService.saveBatch(activityApplyOrgs);
        return row;
    }

    /**
     * 修改注册送券活动
     *
     * @param activityCouponInfoDTO 注册送券活动
     * @return 结果
     */
    @Override
    public int updateActivityCouponInfo(ActivityCouponInfoDTO activityCouponInfoDTO) {
        activityCouponInfoDTO.setUpdateTime(DateUtils.getNowDate());
        ActivityInfo activityInfo = new ActivityInfo();
        BeanUtils.copyProperties(activityCouponInfoDTO, activityInfo);
        int row = activityInfoService.updateActivityInfo(activityInfo);
        ActivityCouponInfo activityCouponInfo = new ActivityCouponInfo();
        BeanUtils.copyProperties(activityCouponInfoDTO, activityCouponInfo, "id");
        activityCouponInfo.setActivityId(activityInfo.getId());
        baseMapper.updateActivityCouponInfo(activityCouponInfo);

        activityApplyOrgService.remove(new QueryWrapper<ActivityApplyOrg>().lambda()
                .eq(ActivityApplyOrg::getActivityId, activityInfo.getId()));

        List<ActivityApplyOrg> activityApplyOrgs = new ArrayList<>();
        for (Long selectStoreId : activityCouponInfoDTO.getSelectStoreIds()) {
            ActivityApplyOrg activityApplyOrg = new ActivityApplyOrg();
            activityApplyOrg.setActivityId(activityInfo.getId());
            activityApplyOrg.setOrgId(selectStoreId);
            activityApplyOrg.setSourceType(SourceType.STORE.getCode());
            activityApplyOrg.setCreateBy(getUsername());
            activityApplyOrg.setCreateById(getUserId());
            activityApplyOrg.setCreateTime(DateUtils.getNowDate());
            activityApplyOrgs.add(activityApplyOrg);
        }
        activityApplyOrgService.saveBatch(activityApplyOrgs);
        return row;
    }

    /**
     * 批量删除注册送券活动
     *
     * @param ids 需要删除的注册送券活动主键
     * @return 结果
     */
    @Override
    public int deleteActivityCouponInfoByIds(Long[] ids, String activityType) {
        activityInfoService.deleteActivityInfoByIds(ids, activityType);
        return baseMapper.deleteActivityCouponInfoByIds(ids);
    }

    /**
     * 删除注册送券活动信息
     *
     * @param id 注册送券活动主键
     * @return 结果
     */
    @Override
    public int deleteActivityCouponInfoById(Long id, String activityType) {
        activityInfoService.deleteActivityInfoById(id, activityType);
        return baseMapper.deleteActivityCouponInfoById(id);
    }

    @Override
    public List<AppCouponType> selectActivityCouponItemList(ActivityAppCouponItemQueryDTO activityAppCouponItemQueryDTO) {
        return baseMapper.selectActivityCouponItemList(activityAppCouponItemQueryDTO,
                SecurityUtils.getLoginUser().getOrgId(),
                SecurityUtils.getLoginUser().getSourceType());
    }

    @Override
    public int countActivityCouponItemList(ActivityAppCouponItemQueryDTO activityAppCouponItemQueryDTO) {
        return baseMapper.countActivityCouponItemList(activityAppCouponItemQueryDTO,
                SecurityUtils.getLoginUser().getOrgId(),
                SecurityUtils.getLoginUser().getSourceType());
    }


}
