package com.goafanti.user.service.impl;

import com.goafanti.admin.service.DepartmentService;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.TOrderMidMapper;
import com.goafanti.common.dao.UserServiceFollowMapper;
import com.goafanti.common.dao.UserServiceMapper;
import com.goafanti.common.model.UserService;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.user.bo.*;
import com.goafanti.user.service.UserServiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class  UserServiceServiceImpl extends BaseMybatisDao<UserServiceMapper> implements UserServiceService {

	@Resource
	private UserServiceMapper userServiceMapper;
	@Resource
	private UserServiceFollowMapper userServiceFollowMapper;
	@Resource
	private DepartmentService departmentService;
	@Resource
	private TOrderMidMapper tOrderMidMapper;

	@Override
	public int addUseService(InputUserServiceFollow us) {
		us.setAid(TokenManager.getAdminId());
		if (StringUtils.isNotBlank(us.getRenewalTimes())) {
			us.setRenewalTime(DateUtils.StringToDate(us.getRenewalTimes(), AFTConstants.YYYYMMDD));
		}
		Date date=new Date();
		us.setCreateTime(date);
		userServiceFollowMapper.insertSelective(us);
		Integer id=userServiceMapper.selectByUid(us.getUid());
		UserService u=new UserService();
		BeanUtils.copyProperties(us, u);
		u.setNewTime(date);
		if (id==null) {
			userServiceMapper.insertSelective(u);
		}else {
			u.setId(id);
			u.setCreateTime(null);
			userServiceMapper.update(u);
		}
		return 1;
	}

	@Override
	public List<OutUserServiceFollow> listUseServiceFollow(String uid) {
		return userServiceFollowMapper.listUseServiceFollow(uid);
	}

	@Override
	public Pagination<OutOrderUserService> listOrderUseService(InputListOrderUserService i) {
		Map<String, Object> map = setParams(i);
		Pagination<OutOrderUserService> p=(Pagination<OutOrderUserService>) findPage("OrderUseServiceList", "OrderUseServiceCount", map, i.getPageNo(), i.getPageSize());
		List<OutOrderUserService> list= (List<OutOrderUserService>) p.getList();
		setOrderUserService(list);
		return p;
	}

	private Map<String, Object> setParams(InputListOrderUserService i) {
		Map<String, Object>map =new HashMap<>();
		if(i.getUserName()!=null)map.put("userName", i.getUserName());
		if(i.getDeps()!=null)map.put("deps", departmentService.parseArray(i.getDeps()));
		if(i.getStartFollowTimes()!=null)map.put("startFollowTimes", i.getStartFollowTimes());
		if(i.getEndFollowTimes()!=null)map.put("endFollowTimes", i.getEndFollowTimes()+" 23:59:59");
		if(i.getStartSignTimes()!=null)map.put("startSignTimes", i.getStartSignTimes());
		if(i.getEndSignTimes()!=null)map.put("endSignTimes", i.getEndSignTimes()+" 23:59:59");
		if(i.getAddWechat()!=null)map.put("addWechat", i.getAddWechat());
		if(i.getRenewal()!=null)map.put("renewal", i.getRenewal());
		if (i.getContractNo()!=null)map.put("contractNo", i.getContractNo());
		if (i.getAddGroupChat()!=null)map.put("addGroupChat", i.getAddGroupChat());
		if (i.getAddGroupChatTimeStart()!=null)map.put("addGroupChatTimeStart", i.getAddGroupChatTimeStart());
		if (i.getAddGroupChatTimeEnd()!=null)map.put("addGroupChatTimeEnd", i.getAddGroupChatTimeEnd()+" 23:59:59");
		return map;
	}

	@Override
	public List<OutOrderUserService> listOrderUseServiceGetList(InputListOrderUserService i) {
		Map<String, Object> map = setParams(i);
		if (i.getPageSize()==null) i.setPageSize(99999);
		List<OutOrderUserService> list=(List<OutOrderUserService>) findList("OrderUseServiceList", map,i.getPageNo(), i.getPageSize());
		setOrderUserService(list);
		return list;
	}



	private void setOrderUserService(List<OutOrderUserService> list) {
		for (OutOrderUserService ou: list) {
			StringBuilder projects=new StringBuilder();
			StringBuilder consultants=new StringBuilder();
			StringBuilder declareBatchs=new StringBuilder();
			StringBuilder certificationNos=new StringBuilder();
			for (OrderProject o : ou.getList()) {
				if (StringUtils.isNotBlank(o.getCommodityName())) {
					projects.append(o.getCommodityName());
				} else {
					projects.append("无");
				}
				projects.append("/");
				if (StringUtils.isNotBlank(o.getReceiverName())) {
					consultants.append(o.getReceiverName());
				} else {
					consultants.append("无");
				}
				consultants.append("/");
				if (o.getDeclarationBatch() != null) {
					declareBatchs.append(o.getDeclarationBatch());
				} else {
					declareBatchs.append("无");
				}
				declareBatchs.append("/");
				if (StringUtils.isNotBlank(o.getCertificateNumber())) {
					certificationNos.append(o.getCertificateNumber());
				} else {
					certificationNos.append("无");
				}
				certificationNos.append("/");
			}
			if(projects.length()>1)ou.setProjects(projects.substring(0, projects.length()-1));
			if(consultants.length()>1)ou.setConsultants(consultants.substring(0, consultants.length()-1));
			if(declareBatchs.length()>1)ou.setDeclareBatchs(declareBatchs.substring(0, declareBatchs.length()-1));
			if(certificationNos.length()>1)ou.setCertificationNos(certificationNos.substring(0, certificationNos.length()-1));
		}
	}

}
