package com.gic.haoban.manage.service.service.out.impl;

import com.gic.commons.util.EntityUtil;
import com.gic.haoban.common.utils.UuidUtil;
import com.gic.haoban.manage.api.dto.AuditSettingDTO;
import com.gic.haoban.manage.api.service.AuditSettingApiService;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanAuditSettingMapper;
import com.gic.haoban.manage.service.entity.TabHaobanAuditSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by tgs on 2020/2/9.
 */
@Service
public class AuditSettingApiServiceImpl implements AuditSettingApiService{
	@Autowired
	private TabHaobanAuditSettingMapper auditSettingMapper;

	@Override
	public void saveSetting(String wxEnterpriseId, int auditFlag,int clerkChangeFlag) {
		TabHaobanAuditSetting tab = auditSettingMapper.findSetting(wxEnterpriseId);
		if(tab == null){
			tab = new TabHaobanAuditSetting();
			tab.setAuditFlag(auditFlag);
			tab.setCreateTime(new Date());
			tab.setUpdateTime(new Date());
			tab.setSettingId(UuidUtil.randomUUID());
			tab.setStatusFlag(1);
			tab.setWxEnterpriseId(wxEnterpriseId);
			tab.setClerkChangeFlag(clerkChangeFlag);
			auditSettingMapper.insert(tab);
		}else{
			tab.setAuditFlag(auditFlag);
			tab.setClerkChangeFlag(clerkChangeFlag);
			tab.setUpdateTime(new Date());
			auditSettingMapper.updateByPrimaryKeySelective(tab);
		}
	}

	@Override
	public AuditSettingDTO findSettingByWxEnterpriseId(
			String wxEnterpriseId) {
		TabHaobanAuditSetting tab = auditSettingMapper.findSetting(wxEnterpriseId);
		if(tab == null){
			AuditSettingDTO dto = new AuditSettingDTO();
			dto.setWxEnterpriseId(wxEnterpriseId);
			dto.setAuditFlag(1);
			dto.setClerkChangeFlag(0);
			return dto;
		}
		int clerkChangeFlag = tab.getClerkChangeFlag()== null?0:tab.getClerkChangeFlag();
		tab.setClerkChangeFlag(clerkChangeFlag);
		AuditSettingDTO dto = EntityUtil.changeEntity(AuditSettingDTO.class, tab);
		return dto;
	}
	
}
