package com.ylx.massage.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.common.constant.MassageConstants;
import com.ylx.common.exception.ServiceException;
import com.ylx.common.utils.bean.BeanUtils;
import com.ylx.massage.domain.TJs;
import com.ylx.massage.domain.TJsDay;
import com.ylx.massage.domain.TXiangmu;
import com.ylx.massage.domain.dto.OptionDTO;
import com.ylx.massage.domain.dto.ServiceOptionDTO;
import com.ylx.massage.domain.vo.ProductOptionVO;
import com.ylx.massage.domain.vo.ProductServiceOptionVO;
import com.ylx.massage.domain.vo.TXiangmuDetailVo;
import com.ylx.massage.mapper.TJsDayMapper;
import com.ylx.massage.mapper.TXiangmuMapper;
import com.ylx.massage.service.TJsService;
import com.ylx.massage.service.TXiangmuService;
import com.ylx.massage.utils.DateTimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 项目Service接口
 *
 * @author ylx
 * @date 2024-03-22
 */
@Service
public class TXiangmuServiceImpl extends ServiceImpl<TXiangmuMapper, TXiangmu> implements TXiangmuService {
    @Resource
    private TJsService jsService;

    @Resource
    private TJsDayMapper jsDayMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addOrUpdate(TXiangmu xiangmu) {
        if (StringUtils.isBlank(xiangmu.getcId())) {
            if (StringUtils.isBlank(xiangmu.getcCover())) {
                throw new ServiceException("封面图不能为空");
            }
            if (StringUtils.isBlank(xiangmu.getcTitle())) {
                throw new ServiceException("标题不能为空");
            }
            if (null == xiangmu.getdPrice()) {
                throw new ServiceException("标注价格（现价）不能为空");
            }
            if (null == xiangmu.getdYuanPrice()) {
                throw new ServiceException("原价不能为空");
            }
            if (null == xiangmu.getnMinute()) {
                throw new ServiceException("时长不能为空");
            }
            xiangmu.setnSaleNumber(MassageConstants.INTEGER_ZERO);
            xiangmu.setDtCreateTime(LocalDateTime.now());
        }
        return this.saveOrUpdate(xiangmu);
    }

    @Override
    public TXiangmuDetailVo details(TXiangmu xiangmu) {
        TXiangmu byId = this.getById(xiangmu.getcId());
        if (null != byId) {
            TXiangmuDetailVo tXiangmuDetailVo = new TXiangmuDetailVo();
            BeanUtils.copyBeanProp(tXiangmuDetailVo, byId);
            //推荐技师
            List<TJsDay> tJsDays = jsDayMapper.selectRanking(xiangmu.getDeptName(), null,
                    DateTimeUtils.formatDate(DateTimeUtils.addMonths(new Date(), -1)), DateTimeUtils.formatDate(new Date()));
            //查技师
            if (CollectionUtil.isEmpty(tJsDays)) {
                return tXiangmuDetailVo;
            }
            List<String> jsId = tJsDays.stream().map(TJsDay::getJsId).collect(Collectors.toList());
            tXiangmuDetailVo.setRecommendTechnicianList(jsId);
            LambdaQueryWrapper<TJs> jsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            jsLambdaQueryWrapper.in(CollectionUtil.isNotEmpty(jsId), TJs::getId, jsId);
            List<TJs> list = jsService.list(jsLambdaQueryWrapper);
            tXiangmuDetailVo.setJsList(list);
            return tXiangmuDetailVo;
        }
        return null;
    }

    @Override
    public IPage<ProductOptionVO> selectOptionUnionPage(Page page, OptionDTO dto) {
        return baseMapper.selectOptionUnionPage(page, dto);
    }

    @Override
    public Page<ProductServiceOptionVO> selectServiceOptionsPage(Page page, ServiceOptionDTO dto) {
        return baseMapper.selectServiceOptionsPage(page, dto);
    }
}
