package com.gxwebsoft.love.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.love.entity.UserPlan;
import com.gxwebsoft.love.entity.UserPlanEquity;
import com.gxwebsoft.love.entity.UserPlanIcon;
import com.gxwebsoft.love.entity.UserPlanPrice;
import com.gxwebsoft.love.mapper.UserPlanMapper;
import com.gxwebsoft.love.param.UserPlanEquityParam;
import com.gxwebsoft.love.param.UserPlanLogParam;
import com.gxwebsoft.love.param.UserPlanParam;
import com.gxwebsoft.love.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会员套餐管理表Service实现
 *
 * @author 科技小王子
 * @since 2023-06-21 11:21:01
 */
@Service
public class UserPlanServiceImpl extends ServiceImpl<UserPlanMapper, UserPlan> implements UserPlanService {
    @Resource
    private UserPlanPriceService userPlanPriceService;
    @Resource
    private UserPlanLogService userPlanLogService;
    @Resource
    private UserPlanEquityService userPlanEquityService;
    @Resource
    private UserPlanIconService userPlanIconService;

    @Override
    public PageResult<UserPlan> pageRel(UserPlanParam param) {
        PageParam<UserPlan, UserPlanParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
        List<UserPlan> list = baseMapper.selectPageRel(page, param);
        list.forEach(d -> {
          d.setPrice(userPlanPriceService.list(new LambdaQueryWrapper<UserPlanPrice>().eq(UserPlanPrice::getPlanId, d.getPlanId())));
          final UserPlanLogParam logParam = new UserPlanLogParam();
          logParam.setPlanId(d.getPlanId());
          logParam.setUserId(param.getLoginUserId());
          d.setLog(userPlanLogService.listRel(logParam));
          d.setIcons(userPlanIconService.list(new LambdaQueryWrapper<UserPlanIcon>().eq(UserPlanIcon::getPlanId,d.getPlanId())));
        });
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<UserPlan> listRel(UserPlanParam param) {
        List<UserPlan> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<UserPlan, UserPlanParam> page = new PageParam<>();
        //page.setDefaultOrder("create_time desc");
        list.forEach(d -> {
          // 套餐价格列表
          d.setPrice(userPlanPriceService.list(new LambdaQueryWrapper<UserPlanPrice>().eq(UserPlanPrice::getPlanId, d.getPlanId())));
          // 购买记录
          final UserPlanLogParam logParam = new UserPlanLogParam();
          logParam.setPlanId(d.getPlanId());
          logParam.setUserId(param.getLoginUserId());
          d.setLog(userPlanLogService.pageRel(logParam).getList());
          d.setIcons(userPlanIconService.list(new LambdaQueryWrapper<UserPlanIcon>().eq(UserPlanIcon::getPlanId,d.getPlanId())));
          // 解锁的功能
          final UserPlanEquityParam userPlanEquityParam = new UserPlanEquityParam();
          userPlanEquityParam.setUserId(param.getLoginUserId());
          userPlanEquityParam.setPlanId(d.getPlanId());
          d.setEquity(userPlanEquityService.listRel(userPlanEquityParam));
        });
        return page.sortRecords(list);
    }

    @Override
    public UserPlan getByIdRel(Integer planId,Integer loginUserId) {
        UserPlanParam param = new UserPlanParam();
        param.setPlanId(planId);
        final List<UserPlan> list = baseMapper.selectListRel(param);
        list.forEach(d -> {
          // 套餐价格列表
          d.setPrice(userPlanPriceService.list(new LambdaQueryWrapper<UserPlanPrice>().eq(UserPlanPrice::getPlanId, d.getPlanId())));
          // 购买记录
          final UserPlanLogParam logParam = new UserPlanLogParam();
          logParam.setPlanId(d.getPlanId());
          logParam.setUserId(loginUserId);
          logParam.setLimit(1L);
          d.setLog(userPlanLogService.pageRel(logParam).getList());
          d.setIcons(userPlanIconService.list(new LambdaQueryWrapper<UserPlanIcon>().eq(UserPlanIcon::getPlanId,d.getPlanId())));
          // 解锁的功能
          final UserPlanEquityParam userPlanEquityParam = new UserPlanEquityParam();
          userPlanEquityParam.setUserId(loginUserId);
          userPlanEquityParam.setPlanId(d.getPlanId());
          d.setEquity(userPlanEquityService.listRel(userPlanEquityParam));
        });
        return param.getOne(list);
    }

}
