package cn.iocoder.yudao.module.inspection.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.iocoder.yudao.util.StringUtils;
import cn.iocoder.yudao.module.inspection.entity.InspectionCategoryTemplate;
import cn.iocoder.yudao.module.inspection.entity.ShopInspectionGoods;
import cn.iocoder.yudao.module.inspection.mapper.ShopInspectionCategoryMapper;
import cn.iocoder.yudao.module.inspection.entity.ShopInspectionCategory;
import cn.iocoder.yudao.module.inspection.service.IInspectionCategoryTemplateService;
import cn.iocoder.yudao.module.inspection.service.IShopInspectionGoodsService;
import cn.iocoder.yudao.module.inspection.service.ShopInspectionCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * (ShopInspectionCategory)表服务实现类
 *
 * @author makejava
 * @since 2023-08-01 19:59:23
 */
@Service("shopInspectionCategoryService")
public class ShopInspectionCategoryServiceImpl extends ServiceImpl<ShopInspectionCategoryMapper, ShopInspectionCategory> implements ShopInspectionCategoryService {

    @Autowired
    private IInspectionCategoryTemplateService templateService;
    @Autowired
    private IShopInspectionGoodsService goodsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveVo(ShopInspectionCategory category) {
        this.save(category);
        List<InspectionCategoryTemplate> templates = category.getTemplates();
        for (InspectionCategoryTemplate template : templates) {
            template.setCategoryId(category.getId());
        }
        templateService.saveBatch(templates);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByIdVo(ShopInspectionCategory category) {
        this.updateById(category);
        templateService.deleteByCategoryId(category.getId());
        List<InspectionCategoryTemplate> templates = category.getTemplates();
        for (InspectionCategoryTemplate template : templates) {
            template.setCategoryId(category.getId());
        }
        templateService.saveBatch(templates);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeByIdsVo(List<Long> idList) throws Exception {
        for (Long id : idList) {
            LambdaQueryWrapper<ShopInspectionGoods> queryWrapper =new LambdaQueryWrapper<>();
            queryWrapper.eq(ShopInspectionGoods::getGoodsCategoryId,id);
            long count = goodsService.count(queryWrapper);
            if (count>0){
                throw new Exception("该分类下有商品不可删除");
            }
            this.removeById(id);
            LambdaQueryWrapper<InspectionCategoryTemplate> queryWrapper2 =new LambdaQueryWrapper<>();
            queryWrapper2.eq(InspectionCategoryTemplate::getCategoryId,id);
            templateService.remove(queryWrapper2);
        }


    }

    @Override
    public PageResult<ShopInspectionCategory> listVo(ShopInspectionCategory shopInspectionCategory, PageParam pageParam) {

        QueryWrapper<ShopInspectionCategory> queryWrapper = new QueryWrapper<>();
        if(null!=shopInspectionCategory.getCategoryName()){
            queryWrapper.like("category_name",shopInspectionCategory.getCategoryName());
        }
        queryWrapper.orderByAsc("order_num");
        return baseMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public ShopInspectionCategory getByIdVo(Long id) {
        ShopInspectionCategory category = this.getById(id);
        LambdaQueryWrapper<InspectionCategoryTemplate> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(InspectionCategoryTemplate::getCategoryId,id).orderByAsc(InspectionCategoryTemplate::getId);
        List<InspectionCategoryTemplate> list = templateService.list(queryWrapper);
        category.setTemplates(list);
        return category;
    }

}

