package com.miaxis.topic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.utils.bean.BeanUtils;
import com.miaxis.file.service.IFileInfoService;
import com.miaxis.goods.domain.GoodsInfo;
import com.miaxis.goods.mapper.GoodsInfoMapper;
import com.miaxis.goods.service.IGoodsInfoService;
import com.miaxis.goods.vo.GoodsInfoVo;
import com.miaxis.topic.domain.TopicInfo;
import com.miaxis.topic.dto.TopicInfoDto;
import com.miaxis.topic.mapper.TopicInfoMapper;
import com.miaxis.topic.service.ITopicInfoService;
import com.miaxis.topic.vo.TopicInfoVo;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 专题Service业务层处理
 *
 * @author miaxis
 * @date 2021-03-11
 */
@Service
@RequiredArgsConstructor
public class TopicInfoServiceImpl extends ServiceImpl<TopicInfoMapper, TopicInfo> implements ITopicInfoService {

    private final TopicInfoMapper topicInfoMapper;

    private final IGoodsInfoService goodsInfoService;

    private final GoodsInfoMapper goodsInfoMapper;

    private final IFileInfoService fileInfoService;

    /**
     * 查询专题列表
     *
     * @param topicInfoVo 专题
     * @return 专题
     */
    @Override
    public List<TopicInfoVo> selectTopicInfoList(TopicInfoVo topicInfoVo){

        //专题列表
        List<TopicInfoVo>  topicInfoVos = topicInfoMapper.selectTopicInfoList(topicInfoVo);

        for (TopicInfoVo topic : topicInfoVos) {
            //查询对应商品
            List<GoodsInfoVo> goodsInfoVoList = goodsInfoMapper.getGoodsInfoListByTopicId(topic.getId());
            if (goodsInfoVoList.size() > 0){
                topic.setGoodsInfoList(goodsInfoVoList);
            }
        }
        return topicInfoVos;
    }

    /**
     * 新增专题
     * @param topicInfoDto
     * @return
     */
    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Response<Integer> addTopic(TopicInfoDto topicInfoDto) {

        //保存专题信息
        TopicInfo topicInfo = new TopicInfo();
        BeanUtils.copyProperties(topicInfoDto,topicInfo);
        this.save(topicInfo);

        //保存商品信息
        List<GoodsInfo> goodsInfoList = topicInfoDto.getGoodsInfoList();
        goodsInfoList.forEach(g -> g.setTopicId(Integer.valueOf(topicInfo.getId().toString())));
        goodsInfoService.saveBatch(goodsInfoList);

        return Response.success();
    }

    /**
     * 删除专题
     * @param ids
     * @return
     */
    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Response<Integer> removeTopicByIds(Long[] ids) {
        for (Long id : ids) {
            //删除专题
            this.update(new UpdateWrapper<TopicInfo>().set("status",1).eq("id",id));
            //删除商品
            goodsInfoService.update(new UpdateWrapper<GoodsInfo>().set("status",1).eq("topic_id",id));
        }
        return Response.success();
    }

    /**
     * 查询专题详情
     * @param id
     * @return
     */
    @Override
    @SneakyThrows
    public Response getTopicDetailsById(Long id) {
        TopicInfoVo topicInfoVo = topicInfoMapper.getTopicDetailsById(id);
        topicInfoVo.setGoodsInfoList(goodsInfoMapper.getGoodsInfoListByTopicId(topicInfoVo.getId()));
        return Response.success(topicInfoVo);
    }

    /**
     * 更新 0：上架 / 1：下架
     * @param ids
     * @param shelfStatus
     * @return
     */
    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public Response updateShelfStatus(Long[] ids, Integer shelfStatus) {
        for (Long id : ids) {
            this.update(new UpdateWrapper<TopicInfo>().set("shelf_status",shelfStatus).eq("id",id));
        }
        return Response.success();
    }


}
