package com.miaxis.fulu.service.impl;

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.core.domain.entity.SysDictData;
import com.miaxis.fulu.domain.FuluCommodityInfo;
import com.miaxis.fulu.mapper.FuluCommodityInfoMapper;
import com.miaxis.fulu.service.IFuluCommodityInfoService;
import com.miaxis.fulu.vo.FuluCommodityInfoAppletVo;
import com.miaxis.fulu.vo.FuluCommodityInfoVo;
import com.miaxis.system.mapper.SysDictDataMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/6/24 15:44
 */
@Service
@AllArgsConstructor
public class FuluCommodityInfoServiceImpl extends ServiceImpl<FuluCommodityInfoMapper, FuluCommodityInfo> implements IFuluCommodityInfoService {

    private final FuluCommodityInfoMapper fuluCommodityInfoMapper;

    private final SysDictDataMapper sysDictDataMapper;

    /**
     * 查询福禄商品列表
     *
     * @param fuluCommodityInfo 福禄商品
     * @return 福禄商品
     */
    @Override
    public List<FuluCommodityInfoVo> selectFuluCommodityInfoList(FuluCommodityInfo fuluCommodityInfo){
        return fuluCommodityInfoMapper.selectFuluCommodityInfoList(fuluCommodityInfo);
    }

    /**
     * 获取福禄商品详细信息
     * @param id
     * @return
     */
    @Override
    public Response<FuluCommodityInfoVo> getFuLuById(Long id) {
        return Response.success(fuluCommodityInfoMapper.getFuLuById(id));
    }

    /**
     * 删除福禄商品（伪删除）
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response removeFuLuByIds(Long[] ids) {
        for (Long id : ids) {
            this.update(new UpdateWrapper<FuluCommodityInfo>().set("status",1).eq("id",id));
        }
        return Response.success();
    }

    /**
     * applet
     * 查询福禄商品列表
     * @return
     */
    @Override
    public Response queryFuluCommodityInfoList() {
        List<FuluCommodityInfoAppletVo> infoAppletVo = new ArrayList<>();
        List<SysDictData> fuluGoodsTypeList = sysDictDataMapper.selectDictDataByType("fulu_goods_type");
        List<FuluCommodityInfoVo> commodityInfos = fuluCommodityInfoMapper.getFuluDataList();

        for (SysDictData sysDictData : fuluGoodsTypeList) {
            FuluCommodityInfoAppletVo infoAppletVo1 = new FuluCommodityInfoAppletVo();
            infoAppletVo1.setTypeName(sysDictData.getDictLabel());
            infoAppletVo1.setList(commodityInfos.stream().filter(m -> m.getFuluGoodsType().toString().equals(sysDictData.getDictValue())).collect(Collectors.toList()));
            infoAppletVo.add(infoAppletVo1);
        }
        return Response.success(infoAppletVo);
    }
}
