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

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

import org.apache.commons.lang3.StringUtils;
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.metadata.IPage;

import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.sys.dao.CallBackDao;
import io.renren.modules.sys.dto.CallBackDTO;
import io.renren.modules.sys.entity.CallBackEntity;
import io.renren.modules.sys.entity.CompaniesEntity;
import io.renren.modules.sys.service.CallBackService;
import io.renren.modules.sys.service.CompaniesService;

/**
 * 反馈表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2022-05-29
 */
@Service
public class CallBackServiceImpl extends CrudServiceImpl<CallBackDao, CallBackEntity, CallBackDTO> implements CallBackService {
	
	@Autowired
	private CompaniesService companiesService;

    @Override
    public QueryWrapper<CallBackEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<CallBackEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }
    
    @Override
    public PageData<CallBackDTO> page(Map<String, Object> params){
    	//回访时间
    	Object callStartDate = params.get("callStartDate");
    	Object callEndDate = params.get("callEndDate");
    	Object companyId = params.get("companyId");
    	
    	if(null != callStartDate && null != callEndDate) {
			params.put("callDateType", 1);//开始时间和结束时间都有，startDate <= createtime <= startDate
		}else if(null != callStartDate){
			params.put("callDateType", 2);//只有开始时间，createtime >= startDate
		}else if(null != callEndDate){
			params.put("callDateType", 3);//只有结束时间，createtime <= startDate
		}
    	
    	//录入时间
    	Object entryStartDate = params.get("entryStartDate");
    	Object entryEndDate = params.get("entryEndDate");
    	if(null != entryStartDate && null != entryEndDate) {
    		params.put("entryDateType", 1);//开始时间和结束时间都有，startDate <= createtime <= startDate
    	}else if(null != entryStartDate){
    		params.put("entryDateType", 2);//只有开始时间，createtime >= startDate
    	}else if(null != entryEndDate){
    		params.put("entryDateType", 3);//只有结束时间，createtime <= startDate
    	}
    	
    	List<Long> companieList = companiesService.getCompaniesListByUserLong();
    	if(null != companyId) {
    		companieList.add(Long.valueOf(companyId.toString()));
    	}
    	params.put("companyIds", companieList);
    	
    	
    	IPage<CallBackDTO> pageList = baseDao.pageList(getPage(params, null, false),params);
    	List<CallBackDTO> records = pageList.getRecords();
    	for (CallBackDTO dto : records) {
    		CompaniesEntity companies = companiesService.selectById(dto.getCompanyId());
    		if(null != companies) {
    			dto.setCompanyName(companies.getName());
    		}
		}
    	
    	return new PageData<>(records, pageList.getTotal());
    }
}