package com.palmnest.service.repair.impl;

import com.palmnest.application.CodeConstants;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.PropertiesKit;
import com.palmnest.application.core.util.PushMessageHelper;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.dao.repair.RepairServiceDao;
import com.palmnest.model.User;
import com.palmnest.model.estateserve.WxUser;
import com.palmnest.model.repair.RepairService;
import com.palmnest.service.UserManager;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.repair.RepairServiceManager;
import com.palmnest.service.wechat.WeChatApiManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("repairServiceManager")
public class RepairServiceManagerImpl extends GenericManagerImpl<RepairService, Long> implements RepairServiceManager {

	private RepairServiceDao repairServiceDao;

	@Autowired
	public void RepairServiceDao(RepairServiceDao repairServiceDao) {
		this.repairServiceDao = repairServiceDao;
		this.dao = repairServiceDao;
	}

	@Autowired
	private UserManager      userManager;
	@Autowired
	private WeChatApiManager weChatApiManager;
	@Autowired
	private ScWxuserManager  scWxuserManager;

	public static String wxdetailurl = PropertiesKit.readPropertiesValue("repairservice.wx.detail.url", "wechat.properties");
	public static String host        = PropertiesKit.readPropertiesValue("hostUrl", "wechat.properties");

	private static final String BXGL = "bxgl";


	@Override
	public Page findPageBy(Map<String, Object> params, int pageSize, int pageNo) {

		return repairServiceDao.findPageBy(params, pageSize, pageNo);
	}

	@Override
	public String getNo() {
		String           zero  = "";
		String           resNo = repairServiceDao.getNo();
		Date             dtnow = new Date();
		SimpleDateFormat sdf   = new SimpleDateFormat("yyyyMMdd");
		if (StringUtil.isNotEmpty(resNo) && sdf.format(dtnow).equals(resNo.split("-")[1])) {
			//当天
			int q = Integer.parseInt(resNo.split("-")[2]);
			q++;
			for (int i = (q + "").length(); i < 4; i++) {
				zero += "0";
			}
			return BXGL + "-" + resNo.split("-")[1] + "-" + zero + q;
		} else {
			//新的
			return BXGL + "-" + sdf.format(dtnow) + "-0001";
		}
	}

	@Async
	@Override
	public void push(Long parseLong) throws Exception {
		RepairService model   = repairServiceDao.get(parseLong);
		List<User>    users   = userManager.findByFunction(Integer.parseInt(model.getOrg_id().toString()), CodeConstants.AUDIT_REPAIRSERVICE);
		String        title   = "报修";
		String        content = "您有新的报修申请！点击查看详情！";
		PushMessageHelper helper=new PushMessageHelper();
		helper.pushMessage(model.getId(), CodeConstants.REPAIR_PUSH_CODE, users, title, content);
	}

	@Async
	@Override
	public void pushWx(RepairService repairService) {
		String url     = host + wxdetailurl.replaceAll("@id", repairService.getId().toString());
		String content = "你的报修申请已完结！<a href='" + url + "'>点击查看详情！</a>";
		WxUser user    = scWxuserManager.get(Integer.parseInt(repairService.getRepair_user_id().toString()));
		weChatApiManager.sendServiceWithOpenId(user.getDevelopId(), Integer.parseInt(repairService.getRepair_user_id().toString()), content);
	}

}
