package cn.ezhizao.project.business.standardCpmpany.service.impl;

import cn.ezhizao.project.business.company.mapper.BizCompanyMapper;
import cn.ezhizao.project.business.companyContactor.domain.BizCompanyContactor;
import cn.ezhizao.project.business.companyContactor.mapper.BizCompanyContactorMapper;
import cn.ezhizao.project.business.companyFollow.domain.BizCompanyFollowDetail;
import cn.ezhizao.project.business.companyFollow.mapper.BizCompanyFollowDetailMapper;
import cn.ezhizao.project.business.standardCpmpany.domain.BizStandardCompany;
import cn.ezhizao.project.business.standardCpmpany.mapper.BizStandardCompanyMapper;
import cn.ezhizao.project.business.standardCpmpany.service.IBizStandardCompanyService;
import cn.ezhizao.project.system.domain.SysUser;
import cn.ezhizao.project.system.mapper.SysUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.List;

/**
 * standardCompanyService业务层处理
 *
 * @author ezhizao
 * @date 2023-10-30
 */
@Service
public class BizStandardCompanyServiceImpl  extends ServiceImpl<BizStandardCompanyMapper, BizStandardCompany> implements IBizStandardCompanyService
{
    @Resource
    private BizStandardCompanyMapper bizStandardCompanyMapper;
    @Resource
    private BizCompanyFollowDetailMapper bizCompanyFollowDetailMapper;
    @Resource
    private BizCompanyContactorMapper bizCompanyContactorMapper;
    @Resource
    private BizCompanyMapper bizCompanyMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private Validator validator;


    @Override
    public BizStandardCompany getById(Long id) {
        BizStandardCompany company = super.getById(id);
        BizCompanyFollowDetail condition = new BizCompanyFollowDetail();
        condition.setCompanyId(id);
//        List<BizCompanyFollowDetail> followDetails = bizCompanyFollowDetailMapper.getList(condition);
        SysUser user = sysUserMapper.selectUserById(company.getLeaderId());
        company.setLeaderName(user!=null?user.getUserName():"");
        user =sysUserMapper.selectUserById(company.getAdviserId());
        company.setAdviserName(user!=null?user.getUserName():"");
        BizCompanyContactor contactorCondition = new BizCompanyContactor();
        contactorCondition.setCompanyId(id);
//        List<BizCompanyContactor> contactors = bizCompanyContactorMapper.getList(contactorCondition);
//        company.setContactors(contactors);
//        company.setFollowDetails(followDetails);
        return company;
    }

    /**
     * 查询standardCompany列表
     *
     * @param bizStandardCompany standardCompany
     * @return standardCompany
     */
    @Override
    public List<BizStandardCompany> getList(BizStandardCompany bizStandardCompany)
    {
        List<BizStandardCompany> list=bizStandardCompanyMapper.getList(bizStandardCompany);
        for (BizStandardCompany biz: list) {
            biz.setAdviserName(bizStandardCompanyMapper.getUserById(biz.getAdviserId()));
            biz.setLeaderName(bizStandardCompanyMapper.getUserById(biz.getLeaderId()));
        }
        return list;
    }

    @Override
    public boolean updateUserId(List<Long> ids, Long userId ,int index)
    {
        for (Long id:ids){
            int num;
            if(index==0)
                num=bizStandardCompanyMapper.updateLeaderId(id,userId);
            else
                num=bizStandardCompanyMapper.updateAdviserId(id,userId);
            if(num==0){
                return false;
            }
        }
        return true;
    }

    /**
     * 物理删除
     * @param bizStandardCompany
     * @return 删除结果
     */
    @Override
    public int physicalDelete(BizStandardCompany bizStandardCompany){ return bizStandardCompanyMapper.physicalDelete(bizStandardCompany); };


}
