package com.fuint.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuint.common.dto.AccountInfo;
import com.fuint.common.dto.IngredientDto;
import com.fuint.common.util.AuthUserUtil;
import com.fuint.common.vo.IngredientVo;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.web.PageResult;
import com.fuint.repository.mapper.MtGoodsMapper;
import com.fuint.repository.mapper.MtMaterialMapper;
import com.fuint.repository.mapper.MtStoreMapper;
import com.fuint.repository.model.MtGoods;
import com.fuint.repository.model.MtIngredient;
import com.fuint.common.service.IngredientService;
import com.fuint.repository.mapper.MtIngredientMapper;
import com.fuint.repository.model.MtMaterial;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 菜品配料表服务接口
 *
 * Created by pzg
 * CopyRight https://www.fuint.cn
 */
@Service
@AllArgsConstructor
public class IngredientServiceImpl extends ServiceImpl<MtIngredientMapper, MtIngredient> implements IngredientService {

    private static final Logger logger = LoggerFactory.getLogger(IngredientServiceImpl.class);

    private MtIngredientMapper mtIngredientMapper;

    private MtStoreMapper mtStoreMapper;

    private MtGoodsMapper mtGoodsMapper;

    private MtMaterialMapper mtMaterialMapper;

    /**
     * 根据条件搜索菜品配料分页列表
     *
     * @param  ingredientDto 查询参数
     * @throws BusinessCheckException
     * @return
     * */
    @Override
    public PageResult<IngredientVo> queryIngredientPageList(IngredientDto ingredientDto) {
        AccountInfo accountInfo = AuthUserUtil.get();
        Assert.isTrue(ObjectUtils.isNotEmpty(accountInfo), "请登录");

        IPage page = new Page(ingredientDto.getPageNo(), ingredientDto.getPageSize());
        IPage<MtIngredient> ingredientIPage = mtIngredientMapper.selectPage(page, new LambdaQueryWrapper<MtIngredient>()
                .eq(MtIngredient::getGoodsId, ingredientDto.getGoodsId()));
        List<IngredientVo> ingredientVos = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(ingredientIPage.getRecords())){
            ingredientVos = BeanUtil.copyToList(ingredientIPage.getRecords(), IngredientVo.class);
        }
        return PageResult.<IngredientVo>builder().data(ingredientVos).currentPage((int) ingredientIPage.getCurrent()).total((int) ingredientIPage.getTotal()).build();
    }


    /**
     * 给菜品添加配料表
     *
     * @param ingredientDtoList 菜品配料表信息
     * @return
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addIngredientForGoods(List<IngredientDto> ingredientDtoList){

        AccountInfo accountInfo = AuthUserUtil.get();
        Assert.isTrue(ObjectUtils.isNotEmpty(accountInfo), "请登录");

        Assert.isTrue(ObjectUtils.isNotEmpty(ingredientDtoList), "参数不能为空");

        Set<String> goodIdSet = ingredientDtoList.stream().map(IngredientDto::getGoodsId).collect(Collectors.toSet());
        Assert.isFalse(goodIdSet.size()!=1, "请选择同一个菜品");

        List<String> materialIdList = ingredientDtoList.stream().map(IngredientDto::getMaterialId).collect(Collectors.toList());
        materialIdList.forEach(materialId -> {
            Assert.isTrue(ObjectUtils.isNotEmpty(materialId), "请选择菜品配料");
        });
        Integer selectCount = mtIngredientMapper.selectCount(new LambdaQueryWrapper<MtIngredient>().in(MtIngredient::getMaterialId, materialIdList));
        Assert.isFalse(selectCount!=ingredientDtoList.size(), "包含不存在的材料");

        List<MtIngredient> ingredientList = BeanUtil.copyToList(ingredientDtoList, MtIngredient.class);
        boolean b = this.saveBatch(ingredientList);
        Assert.isTrue(b, "保存失败");
    }


    /**
     * 根据ID获菜品配料表取息
     *
     * @param id 菜品配料表ID
     * @return
     */
    @Override
    public IngredientVo queryIngredientById(Long id){
        AccountInfo accountInfo = AuthUserUtil.get();
        Assert.isTrue(ObjectUtils.isNotEmpty(accountInfo), "请登录");

        Assert.isTrue(ObjectUtils.isNotEmpty(id), "参数不能为空");

        MtIngredient mtIngredient = mtIngredientMapper.selectById(id);
        Assert.isTrue(ObjectUtils.isNotEmpty(mtIngredient),"该配料不存在");

        return BeanUtil.copyProperties(mtIngredient,IngredientVo.class);
    }


    /**
     * 修改菜品配料表数据
     *
     * @param ingredientDto
     * @throws BusinessCheckException
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IngredientVo updateIngredient(IngredientDto ingredientDto){
        AccountInfo accountInfo = AuthUserUtil.get();
        Assert.isTrue(ObjectUtils.isNotEmpty(accountInfo), "请登录");

        Assert.isTrue(ObjectUtils.isNotEmpty(ingredientDto), "参数不能为空");
        Assert.isTrue(ObjectUtils.isNotEmpty(ingredientDto.getGoodsId()),"请选择菜品");
        Assert.isTrue(ObjectUtils.isNotEmpty(ingredientDto.getMaterialId()),"请选择材料");

        MtIngredient mtIngredient1 = mtIngredientMapper.selectById(ingredientDto.getId());
        Assert.isTrue(ObjectUtils.isNotEmpty(mtIngredient1),"该配料不存在");

        MtGoods mtGoods = mtGoodsMapper.selectById(ingredientDto.getGoodsId());
        Assert.isTrue(ObjectUtils.isNotEmpty(mtGoods),"所选菜品不存在");

        MtMaterial mtMaterial = mtMaterialMapper.selectById(ingredientDto.getMaterialId());
        Assert.isTrue(ObjectUtils.isNotEmpty(mtMaterial),"所选材料不存在");

        MtIngredient mtIngredient = BeanUtil.copyProperties(ingredientDto, MtIngredient.class);
        mtIngredient.setUpdateTime(new Date());
        mtIngredient.setUpdateUserId(accountInfo.getId().toString());

        int i = mtIngredientMapper.updateById(mtIngredient);
        Assert.isFalse(i!=1, "更新失败");
        return BeanUtil.copyProperties(mtIngredient,IngredientVo.class);

    }



    /**
     * 根据ID删除菜品配料表
     *
     * @param id 菜品配料表ID
     * @return
     */
    @Override
    public void deleteIngredientById(Long id) {
        AccountInfo accountInfo = AuthUserUtil.get();
        Assert.isTrue(ObjectUtils.isNotEmpty(accountInfo), "请登录");

        Assert.isTrue(ObjectUtils.isNotEmpty(id), "参数不能为空");

        MtIngredient mtIngredient1 = mtIngredientMapper.selectById(id);
        Assert.isTrue(ObjectUtils.isNotEmpty(mtIngredient1),"该配料不存在");

        int i = mtIngredientMapper.deleteById(id);
        Assert.isFalse(i!=1, "删除失败");
    }














}
