package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsea.svc.base.base.entity.CommonCompany;
import com.winsea.svc.base.base.entity.CommonStaff;
import com.winsea.svc.base.base.service.ICommonCompanyService;
import com.winsea.svc.base.base.service.ICommonStaffService;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.yiliangyiyun.entity.OpenServiceInfo;
import com.yh.saas.plugin.yiliangyiyun.mapper.OpenServiceInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IOpenServiceInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 开通业务表 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2021-10-08
 */
@Service
public class OpenServiceInfoServiceImpl extends ServiceImpl<OpenServiceInfoMapper, OpenServiceInfo> implements IOpenServiceInfoService {
    @Autowired
    private ICommonStaffService commonStaffService;
    @Autowired
    private ICommonCompanyService commonCompanyService;
    /**
     * 开通业务列表
     * @param openServiceInfo
     * @return
     */
    @Override
    public Page<OpenServiceInfo> selectOpenServiceInfo(OpenServiceInfo openServiceInfo) {
        Page<OpenServiceInfo> openServiceInfoPage = this.selectPage(openServiceInfo.getQueryPage(), new EntityWrapper<OpenServiceInfo>()
                .orderBy("update_date", false));
        List<OpenServiceInfo> openServiceInfoList = openServiceInfoPage.getRecords();
        openServiceInfoPage.setRecords(openServiceInfoList);
        return openServiceInfoPage;
    }
    /**
     * 添加
     * @param openServiceInfo
     * @return
     */
    @Override
    public String addInfo(OpenServiceInfo openServiceInfo) {
        // 新增主键id
        openServiceInfo.setId(IdGenerator.generateUUID());
        boolean one = this.insert(openServiceInfo);
        if (one){
           return "OK";
        } else {
            return "NG";
        }
    }

    @Override
    public List<CommonCompany> selectCommonCompany(String phone) {
        List temp=new ArrayList();
        List<CommonStaff> commonStaffList=commonStaffService.selectList(new EntityWrapper<CommonStaff>()
        .eq("staff_mobile_phone",phone));
        if (CollectionUtils.isNotEmpty(commonStaffList)){
            for (CommonStaff commonStaff:commonStaffList){
                CommonCompany commonCompany=commonCompanyService.selectOne(new EntityWrapper<CommonCompany>()
                        .eq("comp_id",commonStaff.getCompId()));
                if(commonCompany.getCompName().contains("中天昊元")){
                    temp.add(commonCompany);
                }
            }
            for (CommonStaff commonStaff:commonStaffList){
                CommonCompany commonCompany=commonCompanyService.selectOne(new EntityWrapper<CommonCompany>()
                        .eq("comp_id",commonStaff.getCompId()));
                if(!commonCompany.getCompName().contains("中天昊元")){
                    temp.add(commonCompany);
                }
            }
        }
        return temp;
    }
}
