package com.zcxk.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcxk.dao.GisSettingMapper;
import com.zcxk.entity.GisSetting;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * Gis设置服务接口
 *
 * @author yjy
 * @since 2020-11-16
 */
@Service
public class GisSettingService extends ServiceImpl<GisSettingMapper, GisSetting> {

    /**
     * 根据机构查询Gis设置数据
     *
     * @param companyOrgId 所属公司
     * @param deptOrgId 所属部门
     * @param serviceType 服务类型
     * @return 单条数据
     */
    public List<GisSetting> selectGis(Integer companyOrgId,Integer deptOrgId,String serviceType){
        List<GisSetting> res=new ArrayList<GisSetting>();
        if(deptOrgId != null) {
            if(serviceType == null){
                res = this.list(new QueryWrapper<GisSetting>().eq("DEPT_ORG_ID",deptOrgId));
            }else{
                res = this.list(new QueryWrapper<GisSetting>().eq("DEPT_ORG_ID",deptOrgId).eq("SERVICE_TYPE",serviceType));
            }
        } else if(companyOrgId != null){
            if(serviceType == null){
                res = this.list(new QueryWrapper<GisSetting>().eq("COMPANY_ORG_ID",companyOrgId));
            }else {
                res = this.list(new QueryWrapper<GisSetting>().eq("COMPANY_ORG_ID",companyOrgId).eq("SERVICE_TYPE",serviceType));
            }

        }
        return res;
    }
    /**
     * 根据机构查询Gis设置数据
     *
     * @param page 分页信息
     * @param companyOrgId 所属公司
     * @param deptOrgId 所属部门
     * @param serviceType 服务类型
     * @return 单条数据
     */
    public IPage<GisSetting> selectPageGis(IPage<GisSetting> page,Integer companyOrgId,Integer deptOrgId,String serviceType){
        IPage<GisSetting> res=new Page<GisSetting>();
        if(deptOrgId != null) {
            if(StringUtils.isEmpty(serviceType)){
                res = this.page(page,new QueryWrapper<GisSetting>().eq("DEPT_ORG_ID",deptOrgId));
            }else{
                res = this.page(page,new QueryWrapper<GisSetting>().eq("DEPT_ORG_ID",deptOrgId).eq("SERVICE_TYPE",serviceType));
            }
        } else if(companyOrgId != null){
            if(StringUtils.isEmpty(serviceType)){
                res = this.page(page,new QueryWrapper<GisSetting>().eq("COMPANY_ORG_ID",companyOrgId));
            }else {
                res = this.page(page,new QueryWrapper<GisSetting>().eq("COMPANY_ORG_ID",companyOrgId).eq("SERVICE_TYPE",serviceType));
            }

        }
        return res;
    }
    /**
     * 保存数据
     *
     * @param listGisSetting
     * @return
     */
    public boolean saveGis(List<GisSetting> listGisSetting) {
        return this.saveOrUpdateBatch(listGisSetting);
    }
    /**
     * 根据条件获取id
     *
     * @param companyOrgId 所属公司
     * @param deptOrgId 所属部门
     * @param serviceType 服务类型
     * @return
     */
    public Integer getId(Integer companyOrgId,Integer deptOrgId,String serviceType) {
        Integer id = null;
        GisSetting res=new GisSetting();
        if(deptOrgId != null) {
            res= this.getOne(new QueryWrapper<GisSetting>().eq("DEPT_ORG_ID",deptOrgId).eq("SERVICE_TYPE",serviceType));
        }else if(companyOrgId != null){
            res= this.getOne(new QueryWrapper<GisSetting>().eq("COMPANY_ORG_ID",companyOrgId).eq("SERVICE_TYPE",serviceType));
        }
        if(res != null) {
            id=res.getId();
        }
        return id;
    }
    /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    public boolean deleteByGisIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}