package com.fdkankan.platform.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.platform.goods.entity.Company;
import com.fdkankan.platform.goods.mapper.ICompanyMapper;
import com.fdkankan.platform.goods.request.RequestCompany;
import com.fdkankan.platform.goods.service.ICompanyService;
import com.fdkankan.platform.goods.vo.CompanyVO;
import com.fdkankan.platform.user.entity.Manager;
import com.fdkankan.platform.user.service.IManagerService;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户企业logo信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-24
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<ICompanyMapper, Company> implements ICompanyService {

    @Autowired
    private IManagerService managerService;

    @Override
    public String saveOrUpdateByParam(RequestCompany param) {
        Company entity = new Company();
        BeanUtils.copyProperties(param, entity);
        if(!this.saveOrUpdate(entity)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }

    @Override
    public Page<CompanyVO> pageList(RequestCompany param) {
        LambdaQueryWrapper<Company> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getTbStatus, TbStatus.VALID.code());
        queryWrapper.orderByDesc(Company::getId);
        if(!StringUtils.isEmpty(param.getCompanyName())){
            queryWrapper.eq(Company::getCompanyName,param.getCompanyName());
        }
        Page<Company> pageInfo = this.page(new Page<>(param.getPageNum(),param.getPageSize()),queryWrapper);

        List<Company> list = pageInfo.getRecords();
        List<CompanyVO> companyVOList = list.parallelStream().map(company -> {
            CompanyVO companyVO = BeanUtil.copyProperties(company, CompanyVO.class);
            if(company.getManagerId() != null){
                Manager managerEntity = managerService.getById(company.getManagerId());
                if(managerEntity != null){
                    companyVO.setManagerName(managerEntity.getUsername());
                }
            }
            return companyVO;
        }).collect(Collectors.toList());

        Page<CompanyVO> result = new Page<>(param.getPageNum(),param.getPageSize(), pageInfo.getTotal());
        result.setRecords(companyVOList);

        return result;
    }

    @Override
    public List<Long> getManagerId() {
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getTbStatus,TbStatus.VALID.code());
        queryWrapper.isNotNull(Company::getManagerId);
        List<Company> list = this.list(queryWrapper);
        List<Long> returnList = new ArrayList<>();
        for (Company company : list) {
            returnList.add(company.getId());
        }
        return returnList;
    }
}
