package com.ichangzuo.system.service.impl;

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.ichangzuo.system.mapper.StyleMapper;
import com.ichangzuo.system.model.bo.StyleRecomStatBO;
import com.ichangzuo.system.model.bo.StyleUpdateBO;
import com.ichangzuo.system.model.entity.Style;
import com.ichangzuo.system.model.entity.StyleRecommend;
import com.ichangzuo.system.model.query.StylePageQuery;
import com.ichangzuo.system.model.vo.StyleStatisticVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class StyleServiceImpl extends ServiceImpl<StyleMapper, Style> {
    private final StyleRecommendServiceImpl recommendService;

    public StyleStatisticVO statistic(){
        StyleStatisticVO vo = new StyleStatisticVO();

        List<StyleRecomStatBO> recStat = baseMapper.statRecommand();
        recStat.forEach(item->{
            switch (item.getTimesignature()) {
                case 0:
                    vo.setTs0(item.getCount());
                    break;
                case 1:
                    vo.setTs1(item.getCount());
                    break;
                case 2:
                    vo.setTs2(item.getCount());
                    break;
                case 3:
                    vo.setTs3(item.getCount());
                    break;
            }
        });
        vo.setTemp(baseMapper.countTemp());

        return vo;
    }

    public IPage<Style> pageStyle(StylePageQuery queryParams){
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();

        Page<Style> page = new Page<>(pageNum, pageSize);
        if(queryParams.getState()!=null && queryParams.getState()==0)
            page.setCountId("pageStyle_COUNT");

        return baseMapper.pageStyle(page,queryParams);
    }

    public boolean updateStyle(StyleUpdateBO bo){
        Integer styleId = bo.getAccompanyId();
        Integer state = bo.getState();
        Integer cost = bo.getCost();

        if(state!=null){
            StyleRecommend styleRecommend = recommendService.selectByStyleId(styleId);
            if(state==0) { //设为推荐
                if (styleRecommend != null)
                    return true;

                styleRecommend = new StyleRecommend();
                styleRecommend.setStyleID(styleId);
                styleRecommend.setValid(1);
                return recommendService.save(styleRecommend);
            }
            else{
                Style style = getById(styleId);
                if(style==null)
                    return false;

                if(styleRecommend!=null){
                    recommendService.removeById(styleRecommend.getId());
                }

                if(!style.getState().equals(state)){
                    style.setState(state);
                    return updateById(style);
                }

                return true;
            }
        }
        else if(cost!=null){
            Style style = getById(styleId);
            if( cost.equals(style.getCost()))
                return true;

            style.setCost(cost);
            return updateById(style);
        }

        return false;
    }
}
