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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.bl.order.dao.UserPackageDetailDao;
import com.sqx.modules.bl.order.entity.UserPackage;
import com.sqx.modules.bl.order.entity.UserPackageDetail;
import com.sqx.modules.bl.order.service.UserPackageDetailService;
import com.sqx.modules.bl.order.service.UserPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class UserPackageDetailServiceImpl extends ServiceImpl<UserPackageDetailDao, UserPackageDetail> implements UserPackageDetailService {

    @Autowired
    UserPackageService userPackageService;

    @Override
    public Result findDetailUsedQuantity(UserPackageDetail userPackageDetail){
        List<UserPackageDetail> list = Lists.newArrayList();
        if(userPackageDetail.getStatus() == 0 || userPackageDetail.getStatus() == 3){
            list = baseMapper.findMassagePackageDetails(userPackageDetail);
        }else{
            list = baseMapper.findDetailUsedQuantity(userPackageDetail);
        }
        return Result.success().put("data",list);
    }

    @Override
    public List<UserPackageDetail> findMassagePackageDetails(UserPackageDetail userPackageDetail){
        return baseMapper.findMassagePackageDetails(userPackageDetail);
    }

    @Override
    public int insert(UserPackageDetail userPackageDetail){
        return baseMapper.insert(userPackageDetail);
    }

    @Override
    public Result findAppUsedQuantity(UserPackageDetail userPackageDetail){
        Map<String, Object> map = new HashMap<String, Object>();
        UserPackage userPackage = userPackageService.getMyPackageById(userPackageDetail.getMainId());
        int type = userPackage.getType();
        List<UserPackageDetail> list = Lists.newArrayList();
        if(type == 105){
            list = baseMapper.findDetailUsedQuantity(userPackageDetail);
        }else if(type == 106){
            list = baseMapper.findLcDetailUsedQuantity(userPackageDetail);
        }else{
            list = baseMapper.findDetailList(userPackageDetail);
        }
        map.put("mainData",userPackage);
        map.put("detailData",list);
        return Result.success().put("data",map);
    }

    @Override
    public Result getMyPackageDetail(UserPackageDetail userPackageDetail){
        if(userPackageDetail.getId()!=null){
            userPackageDetail = baseMapper.getMyPackageDetail(userPackageDetail.getId());
        }else{
            userPackageDetail = baseMapper.getMyPackageDetailNoId(userPackageDetail.getMainId(),userPackageDetail.getMassageTypeId());
        }
        return Result.success().put("data",userPackageDetail);
    }

    @Override
    public UserPackageDetail findMassagePackageDetailsUsed(Long mainId){
        return baseMapper.findMassagePackageDetailsUsed(mainId);
    }

    @Override
    public int deleteDetailByMain(Long mainId){
        return baseMapper.deleteDetailByMain(mainId);
    }

}