package com.goafanti.demand.service.impl;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.DemandOrderLogMapper;
import com.goafanti.common.dao.DemandOrderMapper;
import com.goafanti.common.enums.DeleteStatus;
import com.goafanti.common.enums.DemandOrderStatus;
import com.goafanti.common.model.DemandOrder;
import com.goafanti.common.model.DemandOrderLog;
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.demand.bo.DemandOrderListBo;
import com.goafanti.demand.bo.OrgDemandOrderListBo;
import com.goafanti.demand.bo.UserDemandOrderListBo;
import com.goafanti.demand.service.DemandOrderService;

@Service
public class DemandOrderServiceImpl extends BaseMybatisDao<DemandOrderMapper> implements DemandOrderService {
	@Autowired
	private DemandOrderMapper		demandOrderMapper;
	@Autowired
	private DemandOrderLogMapper	demandOrderLogMapper;

	@Override
	public List<DemandOrder> selectDemandOrderByUidAndDemandId(String uid, String id) {
		return demandOrderMapper.selectDemandOrderByUidAndDemandId(uid, id);
	}

	@Override
	public void saveDemandOrder(DemandOrder demandOrder, DemandOrderLog dol) {
		demandOrder.setId(UUID.randomUUID().toString().toString());
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MILLISECOND, 0);
		demandOrder.setCreateTime(now.getTime());
		demandOrder.setUid(TokenManager.getUserId());
		if (StringUtils.isBlank(demandOrder.getUid())){
			return;
		}
		demandOrder.setDeletedSign(DeleteStatus.UNDELETE.getCode());
		demandOrder.setStatus(DemandOrderStatus.CREATE.getCode());
		demandOrderMapper.insert(demandOrder);

		dol.setId(UUID.randomUUID().toString());
		dol.setDemandOrderId(demandOrder.getId());
		dol.setRecordTime(demandOrder.getCreateTime());
		dol.setStatus(demandOrder.getStatus());
		demandOrderLogMapper.insert(dol);

	}

	@Override
	public DemandOrder selectByPrimaryKey(String id) {
		return demandOrderMapper.selectByPrimaryKey(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<DemandOrderListBo> listOrderList(Integer status, Integer pNo, Integer pSize) {

		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		return (Pagination<DemandOrderListBo>) findPage("findDemandOrderListByPage", "findDemandOrderCount",
				disposeDemandOrderList(null, null, null, status, false), pNo, pSize);
	}

	@Override
	public int updateShutdownByUser(DemandOrder order) {
		order.setStatus(DemandOrderStatus.SHUTDOWN.getCode());

		DemandOrderLog dol = new DemandOrderLog();
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MILLISECOND, 0);
		dol.setId(UUID.randomUUID().toString());
		dol.setStatus(DemandOrderStatus.SHUTDOWN.getCode());
		dol.setDemandOrderId(order.getId());
		dol.setRecordTime(now.getTime());
		demandOrderLogMapper.insert(dol);
		return demandOrderMapper.updateByPrimaryKey(order);
	}

	@Override
	public List<DemandOrder> selectDemandOrderByDemandId(String demandId) {
		return demandOrderMapper.selectDemandOrderByDemandId(demandId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<UserDemandOrderListBo> listUserDemandOrder(String uid, String username, Integer status,
			Integer pNo, Integer pSize) {

		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		return (Pagination<UserDemandOrderListBo>) findPage("findUserDemandOrderListByPage", "findUserDemandOrderCount",
				disposeDemandOrderList(uid, username, null, status, true), pNo, pSize);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OrgDemandOrderListBo> listOrgDemandOrder(String uid, String unitName, Integer status, Integer pNo,
			Integer pSize) {
		if (pNo == null || pNo < 0) {
			pNo = 1;
		}

		if (pSize == null || pSize < 0 || pSize > 10) {
			pSize = 10;
		}
		return (Pagination<OrgDemandOrderListBo>) findPage("findOrgDemandOrderListByPage", "findOrgDemandOrderCount",
				disposeDemandOrderList(uid, null, unitName, status, true), pNo, pSize);
	}

	@Override
	public int updateDemandOrder(DemandOrder order, DemandOrderLog dol, String recordTimeFormattedDate) {
		if (!StringUtils.isBlank(recordTimeFormattedDate)) {
			dol.setOperator(TokenManager.getAdminId());
			dol.setId(UUID.randomUUID().toString());
			dol.setDemandOrderId(order.getId());
			Date recordTime = null;
			try {
				recordTime = DateUtils.parseDate(recordTimeFormattedDate, AFTConstants.YYYYMMDDHHMMSS);
			} catch (ParseException e) {
			}
			dol.setRecordTime(recordTime);
			demandOrderLogMapper.insert(dol);
		}
		return demandOrderMapper.updateByPrimaryKeySelective(order);
	}

	private Map<String, Object> disposeDemandOrderList(String uid, String username, String unitName, Integer status,
			Boolean flag) {
		Map<String, Object> params = new HashMap<>();

		if (null != status) {
			params.put("status", status);
		}

		if (!StringUtils.isBlank(uid)) {
			params.put("uid", uid);
		}

		if (!StringUtils.isBlank(username)) {
			params.put("username", username);
		}

		if (!StringUtils.isBlank(unitName)) {
			params.put("unitName", unitName);
		}
		if (flag) {
			if (!TokenManager.hasRole(AFTConstants.SUPERADMIN)) {
				params.put("techBrokerId", TokenManager.getAdminId());
			}
		} else {
			params.put("uid", TokenManager.getUserId());
		}

		return params;
	}

}
