package com.pj.project.tb_sort_group;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.project.tb_business.TbBusiness;
import com.pj.project.tb_business.TbBusinessMapper;
import com.pj.project.tb_business_sort.TbBusinessSort;
import com.pj.project.tb_business_sort.TbBusinessSortService;
import com.pj.project.tb_item_type.TbItemType;
import com.pj.project.tb_item_type.TbItemTypeService;
import com.pj.project4sp.global.BusinessException;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pj.utils.sg.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Service: tb_sort_group -- 业务限制组
 *
 * @author qzy
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbSortGroupService extends ServiceImpl<TbSortGroupMapper, TbSortGroup> implements IService<TbSortGroup> {

    @Resource
    private TbBusinessSortService tbBusinessSortService;

    @Resource
    private TbItemTypeService tbItemTypeService;

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    TbSortGroupMapper tbSortGroupMapper;

    /**
     * 增
     */
    void add(TbSortGroup t) {
        List<TbBusinessSort> sorts = t.getSorts();
        t.setCreateTime(DateUtil.now());
        this.save(t);
        int sort = 1;
        for (TbBusinessSort tbBusinessSort : sorts) {
            TbBusinessSort checkDb = tbBusinessSortService.findByItemTypeId(tbBusinessSort.getTypeId());
            if (checkDb != null) {
                throw new BusinessException(checkDb.getTypeName() + "已存在其他规则组");
            }
            String typeId = tbBusinessSort.getTypeId();
            TbItemType tbItemType = tbItemTypeService.getById(typeId);
            tbBusinessSort.setSort(sort).setTypeName(tbItemType.getName())
                    .setGroupId(t.getId()).setName(t.getName());
            if (sort == 1) {
                tbBusinessSort.setIntervalTime(0);
            }
            sort++;
        }
        tbBusinessSortService.saveBatch(sorts);
    }

    /**
     * 删
     */
    void delete(Long id) {
        this.removeById(id);
        tbBusinessSortService.removeByGroupId(id);
    }

    /**
     * 改
     */
    void update(TbSortGroup t) {
        this.updateById(t);
        List<TbBusinessSort> sorts = t.getSorts();
        tbBusinessSortService.removeByGroupId(t.getId());
        int sort = 1;
        for (TbBusinessSort tbBusinessSort : sorts) {
            String typeId = tbBusinessSort.getTypeId();
            TbBusinessSort db = tbBusinessSortService.findByItemTypeId(typeId);
            if (db!=null&&!db.getId().equals(tbBusinessSort.getId())){
                throw new BusinessException(tbBusinessSort.getTypeName()+"已存在其他规则组");
            }
            TbItemType tbItemType = tbItemTypeService.getById(typeId);
            tbBusinessSort.setSort(sort).setTypeName(tbItemType.getName())
                    .setGroupId(t.getId()).setName(t.getName());
            if (sort == 1) {
                tbBusinessSort.setIntervalTime(0);
            }
            sort++;
        }
        tbBusinessSortService.saveBatch(sorts);
    }

    /**
     * 查
     */
    TbSortGroup getById(Long id) {
        TbSortGroup tbSortGroup = super.getById(id);
        List<TbBusinessSort> sorts = tbBusinessSortService.findByGroupId(id);
        tbSortGroup.setSorts(sorts);
        return tbSortGroup;
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    List<TbSortGroup> getList(SoMap so) {
        return tbSortGroupMapper.getList(so);
    }


}
