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.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.artificer.entity.CollectArtificer;
import com.sqx.modules.bl.collect.entity.CollectMassage;
import com.sqx.modules.bl.collect.service.CollectMassageService;
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.MassagePackageDetailService;
import com.sqx.modules.bl.massage.service.MassagePackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class MassagePackageServiceDetailImpl extends ServiceImpl<MassagePackageDetailDao, MassagePackageDetail> implements MassagePackageDetailService {

    @Autowired
    private MassagePackageService packageService;

    @Autowired
    private CollectMassageService collectMassageService;

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

    @Override
    public void updatePrice(MassagePackageDetail massagePackageDetail){
        baseMapper.updatePrice(massagePackageDetail);
        //更新主表中的现价
        modifyPackagePrice(massagePackageDetail);
    }

    @Override
    public void updateCount(MassagePackageDetail massagePackageDetail){
        baseMapper.updateCount(massagePackageDetail);
        //更新主表中的现价
        modifyPackagePrice(massagePackageDetail);
    }

    @Override
    public void updateIntervalDays(MassagePackageDetail massagePackageDetail){
        baseMapper.updateIntervalDays(massagePackageDetail);
    }

    @Override
    public void updateStatus(MassagePackageDetail massagePackageDetail){
        baseMapper.updateStatus(massagePackageDetail);
        //更新主表中的现价
        modifyPackagePrice(massagePackageDetail);
    }

    @Override
    public void insertDetail(MassagePackageDetail massagePackageDetail){
        baseMapper.addMassage(massagePackageDetail);
        baseMapper.addDetail(massagePackageDetail);
        //更新主表中的现价
        modifyPackagePrice(massagePackageDetail);
    }

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

    @Override
    public void delete(MassagePackageDetail massagePackageDetail){
        baseMapper.delete(massagePackageDetail);
        //更新主表中的现价
        modifyPackagePrice(massagePackageDetail);
    }

    /**
     * 更新主表中的现价
     * @param massagePackageDetail
     */
    private void modifyPackagePrice(MassagePackageDetail massagePackageDetail){
        MassagePackage mp = baseMapper.findPackagePrice(massagePackageDetail);
        if(mp == null){
            mp = new MassagePackage();
            mp.setId(massagePackageDetail.getMainId());
            mp.setPrice(new BigDecimal(0));
        }
        packageService.updatePrice(mp);
    }

    @Override
    public Result findAppPage(MassagePackageDetail massagePackageDetail){
        Map<String, Object> map = new HashMap<String, Object>();
        MassagePackage massagePackage = packageService.getById(massagePackageDetail.getMainId());
        if(massagePackageDetail.getUserId()!=null){
            //插入浏览历史
            CollectMassage collectMassage=new CollectMassage();
            collectMassage.setClassify(2);
            collectMassage.setUserId(massagePackageDetail.getUserId());
            collectMassage.setProjectId(massagePackageDetail.getMainId());
            collectMassage.setFlag(2);
            collectMassageService.insertCollect(collectMassage);
        }
        map.put("mainData",massagePackage);
        Page<MassagePackageDetail> pages=new Page<>(massagePackageDetail.getPage(),massagePackageDetail.getLimit());
        map.put("detailData",new PageUtils(baseMapper.findPage(pages,massagePackageDetail)));
        return Result.success(map);
    }

    @Override
    public Result getAppPackageDetail(MassagePackageDetail massagePackageDetail){
        massagePackageDetail = baseMapper.getAppPackageDetail(massagePackageDetail.getId());
        return Result.success().put("data",massagePackageDetail);
    }

}