package com.ruoyi.agentrep.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.agentrep.domain.DAgentAuthInfo;
import com.ruoyi.agentrep.mapper.DAgentAuthInfoMapper;
import com.ruoyi.agentrep.service.IDAgentAuthInfoService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;

/**
 * 代理机构报Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-02
 */
@Service
public class DAgentAuthInfoServiceImpl extends ServiceImpl<DAgentAuthInfoMapper, DAgentAuthInfo> implements IDAgentAuthInfoService {

	@Autowired
	private DAgentAuthInfoMapper dAgentAuthInfoMapper;

	@Override
	public List<DAgentAuthInfo> queryAgentRep(DAgentAuthInfo dAgentAuthInfo) {
		// TODO Auto-generated method stub
		List<DAgentAuthInfo> list = new ArrayList<DAgentAuthInfo>();
		String cityCode = null;
		if (SecurityUtils.isNeedFilterByCityCode()) {
			cityCode = SecurityUtils.needFilterByCityCode();
		}
		List<Map> mapList = dAgentAuthInfoMapper.queryAgentRep(dAgentAuthInfo.getBeginDate(), dAgentAuthInfo.getEndDate(), cityCode);
		if (mapList != null && !mapList.isEmpty()) {
			for (Map map : mapList) {
				DAgentAuthInfo auth = new DAgentAuthInfo();

//				   select  agency_name,count(*) all_total,
//				   sum(case patent_type when '1' then 1 when '2' then 1 when '3' then 1 else 0 end ) pub_total
//				   ,sum(case patent_type when '2' then 1 when '3' then 1 when '4' then 1 else 0 end ) auth_total
//				   ,sum(case  when patent_type='4' then 1 else 0 end ) invention_cnt  
//				   ,sum(case  when patent_type='2' then 1 else 0 end ) model_utility_cnt 
//				   ,sum(case  when patent_type='3'   then 1 else 0 end ) appearance_cnt 

				auth.setAgencyName(getStr(map, "agency_name"));
				auth.setPubTotal(getLong(map, "pub_total"));
				auth.setAuthTotal(getLong(map, "auth_total"));
				auth.setInventionCnt(getLong(map, "invention_cnt"));
				auth.setModelUtilityCnt(getLong(map, "model_utility_cnt"));
				auth.setAppearanceCnt(getLong(map, "appearance_cnt"));
				list.add(auth);
			}

		}
		return list;
	}

	private String getStr(Map map, String key) {
		if (map == null || key == null || !map.containsKey(key)) {
			return "";
		}
		return map.get(key).toString();
	}

	private int getInt(Map map, String key) {
		if (map == null || key == null || !map.containsKey(key)) {
			return 0;
		}
		return Integer.parseInt(map.get(key).toString());
	}

	private long getLong(Map map, String key) {
		if (map == null || key == null || !map.containsKey(key)) {
			return 0L;
		}
		return Long.parseLong(map.get(key).toString());
	}

	/**
	 * 查询代理机构报列表
	 *
	 * @param dAgentAuthInfo 代理机构报
	 * @return 代理机构报集合
	 * @date 2023-04-02
	 */
	@Override
	public List<DAgentAuthInfo> queryList(DAgentAuthInfo dAgentAuthInfo) {
		QueryWrapper<DAgentAuthInfo> queryWrapper = Wrappers.query();
		if (StringUtils.isNotEmpty(dAgentAuthInfo.getAgencyName())) {
			queryWrapper.like("agency_name", dAgentAuthInfo.getAgencyName());
		}
		if (StringUtils.isNotNull(dAgentAuthInfo.getPubTotal())) {
			queryWrapper.eq("pub_total", dAgentAuthInfo.getPubTotal());
		}
		if (StringUtils.isNotNull(dAgentAuthInfo.getAuthTotal())) {
			queryWrapper.eq("auth_total", dAgentAuthInfo.getAuthTotal());
		}
		if (StringUtils.isNotNull(dAgentAuthInfo.getInventionCnt())) {
			queryWrapper.eq("invention_cnt", dAgentAuthInfo.getInventionCnt());
		}
		if (StringUtils.isNotNull(dAgentAuthInfo.getModelUtilityCnt())) {
			queryWrapper.eq("model_utility_cnt", dAgentAuthInfo.getModelUtilityCnt());
		}
		if (StringUtils.isNotNull(dAgentAuthInfo.getAppearanceCnt())) {
			queryWrapper.eq("appearance_cnt", dAgentAuthInfo.getAppearanceCnt());
		}
		if (StringUtils.isNotEmpty(dAgentAuthInfo.getStatus())) {
			queryWrapper.eq("status", dAgentAuthInfo.getStatus());
		}
		queryWrapper.orderByDesc("id");
		return this.list(queryWrapper);
	}

}
