package com.goafanti.contract.service.impl;

import com.goafanti.common.dao.ContractLogMapper;
import com.goafanti.common.dao.ContractMapper;
import com.goafanti.common.dao.UserMapper;
import com.goafanti.common.enums.ContractStatus;
import com.goafanti.common.enums.NoticeReadStatus;
import com.goafanti.common.enums.NoticeStatus;
import com.goafanti.common.model.Contract;
import com.goafanti.common.model.ContractLog;
import com.goafanti.common.model.Notice;
import com.goafanti.common.model.User;
import com.goafanti.contract.bo.InputContactLog;
import com.goafanti.contract.service.ContractLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.UUID;

@Service
public class ContractLogServiceImpl implements ContractLogService {
	@Autowired
	private ContractLogMapper	contractLogMapper;
	@Autowired
	private ContractMapper		contractMapper;
	@Autowired
	private UserMapper			userMapper;

	@Override
	public List<ContractLog> selectContractLogByCid(String cid) {
		return contractLogMapper.selectContractLogByCid(cid);
	}

	//to update 18
	@Override
	public void insertCirculation(InputContactLog icl, String[] principals, String recordTimeFormattedDate) {
		/*Date recordTime = null;
		if (!StringUtils.isBlank(recordTimeFormattedDate)) {
			try {
				recordTime = DateUtils.parseDate(recordTimeFormattedDate, AFTConstants.YYYYMMDDHHMMSS);
			} catch (ParseException e) {
			}
		}
		if (ContractStatus.COMPLETE.getCode().equals(icl.getStatus())) {
			Contract c = contractMapper.selectByPrimaryKey(icl.getCid());
			c.setStatus(icl.getStatus());
			c.setCompleteDate(recordTime);
			contractMapper.updateByPrimaryKeySelective(c);
		}
		List<ContractLog> logs = new ArrayList<>();
		List<Notice> notice = new ArrayList<>();
		ContractLog cl = null;
		for (int i = 0; i < principals.length; i++) {
			cl = new ContractLog();
			BeanUtils.copyProperties(icl, cl);
			cl.setId(UUID.randomUUID().toString());
			cl.setOperator(TokenManager.getAdminId());
			cl.setPrincipal(principals[i]);
			cl.setRecordTime(recordTime);
			logs.add(cl);
			notice.add(createNotice(cl));
		}
		contractLogMapper.insertBatch(logs);
		noticeMapper.insertBatch(notice);*/
	}

	//to update 18
	@SuppressWarnings("unused")
	private Notice createNotice(ContractLog l) {
		Contract info = contractMapper.selectByPrimaryKey(l.getCid());
		Notice n = new Notice();
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MILLISECOND, 0);
		n.setId(UUID.randomUUID().toString());
		n.setCreateTime(now.getTime());
		n.setReaded(NoticeReadStatus.UNREAD.getCode());

		User u = userMapper.queryById(info.getUid());
		if (null != u) {
			n.setPid(u.getAid());
		}

		n.setUid(info.getUid());
		n.setRid(info.getId());

		n.setAid(l.getPrincipal());
		n.setContent("编号" + info.getSerialNumber() + "  " + ContractStatus.getStatus(info.getStatus()).getDesc());
		n.setNoticeType(NoticeStatus.CONTRACT.getCode());
		// n.setYear(info.getCognizanceYear());
		return n;
	}

}
