package io.renren.modules.sys.service.impl;

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

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.renren.common.annotation.DataFilter;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.security.SM4Utils;
import io.renren.modules.sys.dao.CustomersDao;
import io.renren.modules.sys.dto.CustomersDTO;
import io.renren.modules.sys.entity.ChannelsEntity;
import io.renren.modules.sys.entity.CompaniesEntity;
import io.renren.modules.sys.entity.CustomersEntity;
import io.renren.modules.sys.entity.LandingPagesEntity;
import io.renren.modules.sys.entity.OperationsEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.ChannelsService;
import io.renren.modules.sys.service.CompaniesService;
import io.renren.modules.sys.service.CustomersService;
import io.renren.modules.sys.service.LandingPagesService;
import io.renren.modules.sys.service.OperationsService;
import io.renren.modules.sys.service.SysUserService;

/**
 * 客户表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2022-05-29
 */
@Service
public class CustomersServiceImpl extends CrudServiceImpl<CustomersDao, CustomersEntity, CustomersDTO> implements CustomersService {
	@Autowired
	private OperationsService operationsService;
	
	@Autowired
	private ChannelsService channelsService;
	
	@Autowired
	private CompaniesService companiesService;
	
	@Autowired
	private LandingPagesService landingPagesService;
	
	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 加解密秘钥
	 */
	@Value("${encrypt.token}")
	private String TRANSMIT_KEY;
	
    @Override
    public QueryWrapper<CustomersEntity> getWrapper(Map<String, Object> params){
        return new QueryWrapper<>();
    }
    
    @DataFilter()
    @Override
    public PageData<CustomersDTO> page(Map<String, Object> params){
    	Object startDate = params.get("startDate");
    	Object endDate = params.get("endDate");
    	Object mobile = params.get("mobile");
		List<Long> companieList = new ArrayList<Long>();
//    	List<Long> companieList = companiesService.getCompaniesListByUserLong();
    	Object companyId = params.get("companyId");

    	if(null != companyId && !companyId.equals("")) {
    		companieList.add(Long.valueOf(companyId.toString()));
    	}else{
//			List<Long> companieList1 = companiesService.getCompaniesListByUserLong();
			companieList=companiesService.getCompaniesListByUserLong();
		}
    	params.put("companyIds", companieList);
    	
    	if(null != mobile) {
    		String encryptMobile = SM4Utils.encryptData_ECB(mobile.toString(), TRANSMIT_KEY);
			params.put("mobile", encryptMobile);
    	}
    	
    	if(null != startDate && null != endDate) {
			params.put("dateType", 1);//开始时间和结束时间都有，startDate <= createtime <= startDate
		}else if(null != startDate){
			params.put("dateType", 2);//只有开始时间，createtime >= startDate
		}else if(null != endDate){
			params.put("dateType", 3);//只有结束时间，createtime <= startDate
		}else {
			params.put("dateType", 4);//开始时间和结束时间都没有，不添加该查询条件
		}
    	
    	IPage<CustomersDTO> pageList = baseDao.pageList(getPage(params, null, false),params);
    	List<CustomersDTO> records = pageList.getRecords();
    	for (CustomersDTO dto : records) {

			String encryptMobile = SM4Utils.decryptData_ECB(dto.getMobile(), TRANSMIT_KEY);
			String phoneResult = encryptMobile.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
			dto.setMobile(phoneResult);
    		OperationsEntity operations = operationsService.selectById(dto.getOperationId());
    		if(null != operations) {
    			dto.setOperationName(operations.getName());
    		}
    		
    		ChannelsEntity channels = channelsService.selectById(dto.getChannelId());
    		if(null != channels) {
    			dto.setChannelName(channels.getName());
    		}
    		
    		CompaniesEntity companies = companiesService.selectById(dto.getCompanyId());
    		if(null != companies) {
    			dto.setCompanyName(companies.getName());
    		}
    		
    		LandingPagesEntity landingPage = landingPagesService.selectById(dto.getPageId());
    		if(null != landingPage) {
    			dto.setPageName(landingPage.getName());
    		}
    		
    		SysUserEntity creatUser = sysUserService.selectById(dto.getCreator());
    		if(null != creatUser) {
    			dto.setCreatorName(creatUser.getRealName());
    		}
    		
    		SysUserEntity updateUser = sysUserService.selectById(dto.getUpdateBy());
    		if(null != updateUser) {
    			dto.setUpdateName(updateUser.getRealName());
    		}
    		
		}
    	
    	return new PageData<>(records, pageList.getTotal());
    }

}