package com.goafanti.admin.service.impl;

import com.goafanti.admin.bo.SalesTeamBo;
import com.goafanti.admin.service.SalesTeamService;
import com.goafanti.common.dao.SalesTeamMapper;
import com.goafanti.common.model.SalesTeam;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 营销团队(SalesTeam)表服务实现类
 *
 * @author makejava
 * @since 2024-12-09 11:18:44
 */
@Service("salesTeamService")
public class SalesTeamServiceImpl extends BaseMybatisDao<SalesTeamMapper> implements SalesTeamService {
    @Resource
    private SalesTeamMapper salesTeamMapper;


    @Override
    public Pagination<SalesTeamBo> pageList(SalesTeam salesTeam, Integer pageNo, Integer pageSize) {
        return (Pagination<SalesTeamBo>) findPage("findSalesTeamList",
                "findSalesTeamCount", salesTeam, pageNo, pageSize);
    }



    @Override
    public boolean checkName(SalesTeam in) {
        SalesTeam salesTeam = salesTeamMapper.queryByName(in.getName());
        if (salesTeam != null){
            return !salesTeam.getId().equals(in.getId());
        }
        return false;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SalesTeamBo queryById(Integer id) {
        return this.salesTeamMapper.queryDetailsById(id);
    }


    /**
     * 新增数据
     *
     * @param salesTeam 实例对象
     * @return 实例对象
     */
    @Override
    public SalesTeam insert(SalesTeam salesTeam) {
        salesTeam.setCreateTime(new Date());
        if (salesTeam.getSuperId() == null) {
            salesTeam.setAncestors("0");
        }else {
            SalesTeam superSt = salesTeamMapper.queryById(salesTeam.getSuperId());
            if (superSt != null) {
                salesTeam.setAncestors(superSt.getAncestors() + "," + superSt.getId());
            }
        }
        this.salesTeamMapper.insert(salesTeam);
        return salesTeam;
    }

    /**
     * 修改数据
     *
     * @param salesTeam 实例对象
     * @return 实例对象
     */
    @Override
    public SalesTeam update(SalesTeam salesTeam) {
        if (salesTeam.getSuperId() == null) {
            salesTeam.setAncestors("0");
        }else {
            SalesTeam superSt = salesTeamMapper.queryById(salesTeam.getSuperId());
            if (superSt != null) {
                salesTeam.setAncestors(superSt.getAncestors() + "," + superSt.getId());
            }
        }
        this.salesTeamMapper.update(salesTeam);
        return this.queryById(salesTeam.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.salesTeamMapper.deleteById(id) > 0;
    }

    @Override
    public Object list(Integer lvl) {
        return salesTeamMapper.findSalesTeamLvlList(lvl);
    }

    @Override
    public Object lvlList() {
        List<SalesTeamBo> list = salesTeamMapper.findSalesTeamLvlList(null);
        List<SalesTeamBo> lvl1List = list.stream()
                .filter(e -> e.getLvl() == 1)
                .collect(Collectors.toList());
        List<SalesTeamBo> lvl2List = list.stream()
                .filter(e -> e.getLvl() == 2)
                .collect(Collectors.toList());
        List<SalesTeamBo> lvl3List = list.stream()
                .filter(e -> e.getLvl() == 3)
                .collect(Collectors.toList());
        lvl2List.forEach(e -> {
            e.setList(lvl3List.stream()
                    .filter(e1 -> e1.getSuperId().equals(e.getId()))
                    .collect(Collectors.toList()));
        });
        lvl1List.forEach(e -> {
            e.setList(lvl2List.stream()
                   .filter(e1 -> e1.getSuperId().equals(e.getId()))
                   .collect(Collectors.toList()));
        });
        return lvl1List;
    }
}
