package com.sqx.modules.bl.massage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.bl.collect.entity.CollectMassage;
import com.sqx.modules.bl.collect.service.CollectMassageService;
import com.sqx.modules.bl.massage.dao.MassagePackageDao;
import com.sqx.modules.bl.massage.dao.MassagePackageDetailDao;
import com.sqx.modules.bl.massage.entity.MassagePackage;
import com.sqx.modules.bl.massage.entity.MassagePackageDetail;
import com.sqx.modules.bl.massage.service.MassagePackageService;
import com.sqx.modules.bl.order.entity.UserPackageOrder;
import com.sqx.modules.bl.order.service.UserPackageOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MassagePackageServiceImpl extends ServiceImpl<MassagePackageDao, MassagePackage> implements MassagePackageService {

    @Autowired
    UserPackageOrderService orderService;

    @Autowired
    private CollectMassageService collectMassageService;

    @Autowired
    private UserService userService;

    @Autowired
    MassagePackageDetailDao massagePackageDetailDao;

    @Override
    public Result findPage(MassagePackage massagePackage){
        Page<MassagePackage> pages=new Page<>(massagePackage.getPage(),massagePackage.getLimit());
        return Result.success().put("data",new PageUtils(baseMapper.findPage(pages,massagePackage)));
    }

    @Override
    public int updateSales(MassagePackage massagePackage){
        return baseMapper.updateSales(massagePackage);
    }

    @Override
    public int updateEsteemRate(MassagePackage massagePackage){
        return baseMapper.updateEsteemRate(massagePackage);
    }

    @Override
    public int updateCoupon(MassagePackage massagePackage){
        return baseMapper.updateCoupon(massagePackage);
    }

    @Override
    public int updateVip(MassagePackage massagePackage){
        return baseMapper.updateVip(massagePackage);
    }
    @Override
    public int updateNewer(MassagePackage massagePackage){
        return baseMapper.updateNewer(massagePackage);
    }
    @Override
    public int updateStatus(MassagePackage massagePackage){
        return baseMapper.updateStatus(massagePackage);
    }

    @Override
    public int updatePrice(MassagePackage massagePackage){
        return baseMapper.updatePrice(massagePackage);
    }

    @Override
    public int delete(MassagePackage massagePackage){
        return baseMapper.delete(massagePackage);
    }

    @Override
    public Result findPackageAndMassagePage(MassagePackage massagePackage){
        Page<MassagePackage> pages=new Page<>(massagePackage.getPage(),massagePackage.getLimit());
        return Result.success().put("data",new PageUtils(baseMapper.findPackageAndMassagePage(pages,massagePackage)));
    }

    @Override
    public Result findAppActivityPage(MassagePackage massagePackage){
        massagePackage.setStartTime(DateUtils.format(new Date()));
        Page<MassagePackage> pages=new Page<>(massagePackage.getPage(),massagePackage.getLimit());
        return Result.success().put("data",new PageUtils(baseMapper.findAppActivityPage(pages,massagePackage)));
    }

    /**
     * 查询团购详情
     * @param userId
     * @param massagePackage
     * @return 团购详情（拼团人数，正在拼团订单，正在拼团人，时间，差几人拼成）
     */
    @Override
    public Result getAppGroupDetail(Long userId,MassagePackage massagePackage){
        //插入浏览历史
        CollectMassage collectMassage=new CollectMassage();
        collectMassage.setClassify(2);
        collectMassage.setUserId(userId);
        collectMassage.setProjectId(massagePackage.getId());
        collectMassage.setFlag(3);
        collectMassageService.insertCollect(collectMassage);
        Map<String, Object> map = new HashMap<String, Object>();
        MassagePackage entity = baseMapper.getAppActivityDetail(massagePackage.getId());
        List<UserPackageOrder> groupNoList = orderService.getActivityOrderList(massagePackage.getId(),4);
        int groupNum = entity.getGroupNum();
        Map<Long,String> userMap = new HashMap<Long,String>();
        if(groupNoList.size()>0){
            for(int i=0;i< groupNoList.size();i++){
                UserPackageOrder userPackageOrder = groupNoList.get(i);
                userPackageOrder.setStartTime(entity.getStartTime());
                userPackageOrder.setEndTime(entity.getEndTime());
                int lessNum = userPackageOrder.getLessNum();
                lessNum = groupNum - lessNum;
                userPackageOrder.setLessNum(lessNum);
                String groupNo = userPackageOrder.getGroupNo();
                List<UserPackageOrder> orderList = orderService.getActivityOrderListByGroupNo(groupNo);
                if(orderList.size()>0){
                    for(UserPackageOrder order : orderList){
                        UserEntity user = userService.selectUserById(order.getUserId());
                        String phone = user.getPhone().trim();
                        phone = phone.substring(phone.length()-4);
                        order.setPhone(phone);
                        order.setAvatar(user.getAvatar());
                        userMap.put(order.getUserId(),phone);
                    }
                    userPackageOrder.setOrderList(orderList);
                }
            }
        }
        entity.setBuyGroupNum(userMap.size());
        map.put("groupData",entity);
        map.put("ordersData",groupNoList);
        Page<MassagePackageDetail> pages=new Page<>(1,1);
        MassagePackageDetail massagePackageDetail = new MassagePackageDetail();
        massagePackageDetail.setMainId(massagePackage.getId());
        massagePackageDetail.setStatus(1);
        map.put("detailData",new PageUtils(massagePackageDetailDao.findPage(pages,massagePackageDetail)));
        return Result.success(map);
    }

    /**
     * 查询限时秒杀详情
     * @param userId
     * @param massagePackage
     */
    @Override
    public Result getAppFlashDetail(Long userId,MassagePackage massagePackage){
        Map<String, Object> map = new HashMap<String, Object>();
        //插入浏览历史
        CollectMassage collectMassage=new CollectMassage();
        collectMassage.setClassify(2);
        collectMassage.setUserId(userId);
        collectMassage.setProjectId(massagePackage.getId());
        collectMassage.setFlag(4);
        collectMassageService.insertCollect(collectMassage);
        MassagePackage entity = baseMapper.getAppActivityDetail(massagePackage.getId());
        map.put("data",entity);
        Page<MassagePackageDetail> pages=new Page<>(1,1);
        MassagePackageDetail massagePackageDetail = new MassagePackageDetail();
        massagePackageDetail.setMainId(massagePackage.getId());
        massagePackageDetail.setStatus(1);
        map.put("detailData",new PageUtils(massagePackageDetailDao.findPage(pages,massagePackageDetail)));
        return Result.success(map);
    }

}