package com.pj.tb_fee_item;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pj.current.satoken.StpUserUtil;
import com.pj.tb_item_rules.TbItemRules;
import com.pj.tb_item_rules.TbItemRulesService;
import com.pj.utils.sg.AjaxError;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Service: tb_fee_item -- 费项管理
 *
 * @author yzs
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbFeeItemService extends ServiceImpl<TbFeeItemMapper, TbFeeItem> implements IService<TbFeeItem> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    TbFeeItemMapper tbFeeItemMapper;
    @Resource
    private TbItemRulesService tbItemRulesService;

    /**
     * 增
     */
    void add(TbFeeItem t) {
        List<TbItemRules> rules = t.getRules();
        if (rules.isEmpty()){
            throw new AjaxError("规则不能为空");
        }
        t.setCreateTime(new Date()).setUpdateTime(new Date());
        save(t);
        for (TbItemRules rule : rules) {
            rule.setItemId(t.getId());
        }
        tbItemRulesService.saveBatch(rules);
    }

    /**
     * 删
     */
    void delete(Long id) {
        removeById(id);
        tbItemRulesService.removeByItemId(id);
    }

    /**
     * 改
     */
    void update(TbFeeItem t) {
        t.setCreateTime(new Date()).setUpdateTime(new Date())
                .setUpdateBy(StpUserUtil.getLoginIdAsString())
                .setUpdateByName(StpUserUtil.getLoginName());
        updateById(t);
        tbItemRulesService.removeByItemId(t.getId());
        List<TbItemRules> rules = t.getRules();
        if (rules.isEmpty()){
            throw new AjaxError("规则不能为空");
        }
        for (TbItemRules rule : rules) {
            rule.setItemId(t.getId());
        }
        tbItemRulesService.saveBatch(rules);
    }

    /**
     * 查
     */
    TbFeeItem getById(Long id) {
        TbFeeItem tbFeeItem= super.getById(id);
       List<TbItemRules>rules= tbItemRulesService.findByItemId(id);
       tbFeeItem.setRules(rules);
        return tbFeeItem;
    }

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

    /**
     * 查询所有税费
     *
     * @return
     */
    public List<TbFeeItem> getFeeItems() {
        SoMap so = new SoMap();
        so.put("status", 1);
        return tbFeeItemMapper.getList(so);
    }

    /**
     * 根据收费公司查找
     *
     * @param companyId
     * @return
     */
    public List<TbFeeItem> findByCompanyId(Long companyId) {
        QueryWrapper<TbFeeItem> ew = new QueryWrapper<>();
        ew.lambda().eq(TbFeeItem::getCompanyId, companyId);
        return list(ew);
    }
}
