package cn.pluss.platform.userAreaRewardFlow.impl;

import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.entity.UserAreaRewardFlow;
import cn.pluss.platform.entity.UserRewardInfo;
import cn.pluss.platform.mapper.UserAreaRewardFlowMapper;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.rewardConfig.RewardConfigService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.userAreaRewardFlow.UserAreaRewardFlowService;
import cn.pluss.platform.userRewardInfo.UserRewardInfoService;
import cn.pluss.platform.util.DateUtils;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户区域流水 服务实现类
 * </p>
 *
 * @author bzg
 * @since 2022-03-01
 */
@Service
public class UserAreaRewardFlowServiceImpl extends ServiceImpl<UserAreaRewardFlowMapper, UserAreaRewardFlow> implements UserAreaRewardFlowService {


    @Resource
    private UserAppService userAppService;

    @Resource
    private UserRewardInfoService userRewardInfoService;

    @Resource
    private MerchantBaseInfoService baseInfoService;

    @Resource
    private MerchantOrderService merchantOrderService;

    @Resource
    private RewardConfigService rewardConfigService;

    /**
     * 根据用户ID查询流水
     * @date: 2022/3/1 16:20
     * @param userId:
     * @return java.util.List<cn.pluss.platform.entity.UserAreaRewardFlow>
     */
    @Override
    public List<UserAreaRewardFlow> selectListByUserId(Long userId) {
        QueryWrapper<UserAreaRewardFlow> queryWrapper = new QueryWrapper<UserAreaRewardFlow>().eq("userId",userId);
        return baseMapper.selectList(queryWrapper);
    }


    /**
     * 统计用户区域奖励定 统计用户区域奖励流水
     * @date: 2022/3/1 15:09
     */
    @Override
    public void calcUserAreaRewardFlow() {
        QueryWrapper<UserApp> queryWrapper = new QueryWrapper<UserApp>().eq("areaMark","1");
        List<UserApp> list = userAppService.list(queryWrapper);
        if(list.isEmpty()){
            return;
        }
        for (UserApp app:list) {
            if(StringUtil.isEmpty(app.getRoleCode())){
                continue;
            }
            List<UserRewardInfo> areaRewardList = userRewardInfoService.selectListByUserIdAndRole(app.getUserId(),app.getRoleCode());
            if(areaRewardList.isEmpty()){
                //没有配置区域 关闭当前用户区域配置
                app.setAreaMark(0);
                userAppService.updateById(app);
                continue;
            }
            BigDecimal scale = areaRewardList.get(0).getScale();
            List<String> areaList = new ArrayList<>(10);
            for (UserRewardInfo a:areaRewardList) {
                areaList.add(a.getAreaNo());
            }
            List<String> mercList = baseInfoService.getAreaAndRoleList(areaList,app.getRoleCode());
            if(mercList.isEmpty()){
                continue;
            }
            String date = DateUtils.formatDateDefault(DateUtils.add(Calendar.DAY_OF_MONTH, new Date(), -1), "yyyy-MM-dd");
            List<UserAreaRewardFlow> areaFlowList = selectListByUserId(app.getUserId());
            Integer flag = 1;
            if(areaFlowList.isEmpty()){
                flag = -1;
            }
            BigDecimal flow = merchantOrderService.getAreaOrderCount(mercList,date,flag);
            BigDecimal rewardAmt =  scale.multiply(flow).setScale(2,BigDecimal.ROUND_DOWN);
            UserAreaRewardFlow areaFlow = new UserAreaRewardFlow(app,flow,rewardAmt);
            this.baseMapper.insert(areaFlow);
        }
    }
}
