package com.dhcc.finance.main.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.common.Shift;
import com.dhcc.common.constant.SysConstants;
import com.dhcc.common.constant.SysStatusCode;
import com.dhcc.common.model.CurrencyMeta;
import com.dhcc.common.model.SysUserInfo;
import com.dhcc.common.model.response.RestfulResponse;
import com.dhcc.common.util.CacheDataUtil;
import com.dhcc.common.util.SnowflakeIdWorker;
import com.dhcc.finance.main.dao.CwBdAccperiodMapper;
import com.dhcc.finance.main.dao.CwBdAccperiodmonthMapper;
import com.dhcc.finance.main.dao.CwBdAccperiodquartMapper;
import com.dhcc.finance.main.dao.CwBdAccperiodyearMapper;
import com.dhcc.finance.main.domain.CwBdAccperiod;
import com.dhcc.finance.main.domain.CwBdAccperiodmonth;
import com.dhcc.finance.main.domain.CwBdAccperiodquart;
import com.dhcc.finance.main.domain.CwBdAccperiodyear;
import com.dhcc.finance.main.service.AccountingPeriodService;
import com.dhcc.finance.util.LastAndBeginYear;
import com.dhcc.finance.util.MetaDataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import brave.Tracer;
@Service
public class AccountingPeriodServiceImpl implements AccountingPeriodService{
	@Autowired
	private CwBdAccperiodmonthMapper cwBdAccperiodmonthMapper;
	@Autowired
	private CwBdAccperiodquartMapper cwBdAccperiodquartMapper;
	@Autowired
	private CwBdAccperiodyearMapper cwBdAccperiodyearMapper;
	@Autowired
	private CwBdAccperiodMapper cwBdAccperiodMapper;
	@Autowired
	private Tracer tracer;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdAccperiodyear</p>  
	 * <p>Description:查询会计年份表数据 </p>
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @param pkAccperiod
	 * @return  
	 * @see com.dhcc.finance.main.service.AccountingPeriodService#searchCwBdAccperiodyear(int, int, int, java.lang.String)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:05:56
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<PageInfo<CwBdAccperiodyear>, CurrencyMeta> searchCwBdAccperiodyear(int pageNum, int pageSize, int navigatePages, String pkAccperiod) {
		PageHelper.startPage(pageNum, pageSize);
		if(pkAccperiod==null) {
			pkAccperiod=String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
		}
		List<CwBdAccperiodyear> list = cwBdAccperiodyearMapper.selectBy(pkAccperiod);
		PageInfo<CwBdAccperiodyear> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdAccperiodyear>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdAccperiodquart</p>  
	 * <p>Description:查询会计季度表数据 </p>
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @param pkAccperiod
	 * @return  
	 * @see com.dhcc.finance.main.service.AccountingPeriodService#searchCwBdAccperiodquart(int, int, int, java.lang.String)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:06:46
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<PageInfo<CwBdAccperiodquart>, CurrencyMeta> searchCwBdAccperiodquart(int pageNum, int pageSize, int navigatePages, String pkAccperiod) {
		PageHelper.startPage(pageNum, pageSize);
		if(pkAccperiod==null) {
			pkAccperiod=String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
		}
		List<CwBdAccperiodquart> list = cwBdAccperiodquartMapper.selectBy(pkAccperiod);
		PageInfo<CwBdAccperiodquart> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdAccperiodquart>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdAccperiodmonth</p>  
	 * <p>Description: 查询会计月份表数据</p>
	 * @param pageNum
	 * @param pageSize
	 * @param navigatePages
	 * @param pkAccperiod
	 * @return  
	 * @see com.dhcc.finance.main.service.AccountingPeriodService#searchCwBdAccperiodmonth(int, int, int, java.lang.String)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:07:06
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public RestfulResponse<PageInfo<CwBdAccperiodmonth>, CurrencyMeta> searchCwBdAccperiodmonth(int pageNum, int pageSize, int navigatePages, String pkAccperiod) {
		PageHelper.startPage(pageNum, pageSize);
		if(pkAccperiod==null) {
			pkAccperiod=String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
		}
		List<CwBdAccperiodmonth> list = cwBdAccperiodmonthMapper.selectBy(pkAccperiod);
		PageInfo<CwBdAccperiodmonth> pageInfo = new PageInfo<>(list, navigatePages);
		return new RestfulResponse<PageInfo<CwBdAccperiodmonth>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,pageInfo,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: searchCwBdAccperiod</p>  
	 * <p>Description: 查询所有年份</p>
	 * @return  
	 * @see com.dhcc.finance.main.service.AccountingPeriodService#searchCwBdAccperiod()    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:07:20
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor=Exception.class)
	public RestfulResponse<List<CwBdAccperiod>, CurrencyMeta> searchCwBdAccperiod() {
		List<CwBdAccperiod> list = cwBdAccperiodMapper.selectCwBdAccperiod();
		return new RestfulResponse<List<CwBdAccperiod>, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,list,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}
	/*
	 * (非 Javadoc)  
	 * <p>Title: addAccountingYear</p>  
	 * <p>Description:增加年份 </p>
	 * @param year
	 * @return  
	 * @see com.dhcc.finance.main.service.AccountingPeriodService#addAccountingYear(java.lang.String)    
	 * @author huangpeng
	 * @date 2019年3月27日 下午2:07:31
	 * @修改日志：
	 */
	@Override
	@Transactional(rollbackFor=Exception.class)
	public RestfulResponse<Object, CurrencyMeta> addAccountingYear(String year) {
		int res = 0;
		String pk=SnowflakeIdWorker.getInstance().nextIdString();
		SysUserInfo sysUserInfo = CacheDataUtil.<SysUserInfo>getCacheBean(stringRedisTemplate,SysConstants.REDIS_KEY_PREFIX.SYS_USER_INFO,SysUserInfo.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		CwBdAccperiod cwBdAccperiod = new CwBdAccperiod();
		CwBdAccperiodmonth cwBdAccperiodmonth = new CwBdAccperiodmonth();
		CwBdAccperiodquart cwBdAccperiodquart = new CwBdAccperiodquart();
		CwBdAccperiodyear cwBdAccperiodyear = new CwBdAccperiodyear();
		if( sysUserInfo != null ){
			cwBdAccperiod.setCreateUser(sysUserInfo.getUserId());
			cwBdAccperiodmonth.setCreateUser(sysUserInfo.getUserId());
			cwBdAccperiodquart.setCreateUser(sysUserInfo.getUserId());
			cwBdAccperiodyear.setCreateUser(sysUserInfo.getUserId());
			cwBdAccperiod.setUpdateUser(sysUserInfo.getUserId());
			cwBdAccperiodmonth.setUpdateUser(sysUserInfo.getUserId());
			cwBdAccperiodquart.setUpdateUser(sysUserInfo.getUserId());
			cwBdAccperiodyear.setUpdateUser(sysUserInfo.getUserId());
		}
		//年度表
		cwBdAccperiod.setPkAccperiod(pk);
		cwBdAccperiod.setPeriodyear(year);
		cwBdAccperiod.setBegindate(LastAndBeginYear.getYearFirst(Integer.parseInt(year)));
		cwBdAccperiod.setEnddate(LastAndBeginYear.getYearLast(Integer.parseInt(year)));
		cwBdAccperiod.setCreateTime(new Date());
		cwBdAccperiod.setUpdateTime(new Date());
		cwBdAccperiod.setUseState("1");
		cwBdAccperiodMapper.insert(cwBdAccperiod);
		//会计季度表
		int a=1;
		for (int i = 1; i <=4; i++) {
			cwBdAccperiodquart.setPkAccperiodquart(SnowflakeIdWorker.getInstance().nextIdString());
			cwBdAccperiodquart.setPkAccperiod(pk);
			cwBdAccperiodquart.setAccperiodquarter("0"+i);
			cwBdAccperiodquart.setBeginmonth(((a>=10)?"":"0")+a);
			a+=3;
			cwBdAccperiodquart.setEndmonth(((a>10)?"":"0")+(a-1));
			cwBdAccperiodquart.setYearquarter(year+"-0"+i);
			cwBdAccperiodquart.setUseState("1");
			cwBdAccperiodquart.setCreateTime(new Date());
			cwBdAccperiodquart.setUpdateTime(new Date());
			cwBdAccperiodquartMapper.insert(cwBdAccperiodquart);
		}
		//会计半年度
		int b=1;
		for (int i = 1; i <=2; i++) {
			cwBdAccperiodyear.setPkAcchalfyear(SnowflakeIdWorker.getInstance().nextIdString());
			cwBdAccperiodyear.setPkAccperiod(pk);
			cwBdAccperiodyear.setAcchalfyear("0"+i);
			cwBdAccperiodyear.setBeginmonth("0"+b);
			b+=5;
			cwBdAccperiodyear.setEndmonth(((b>=12)?"":"0")+b);
			b+=1;
			cwBdAccperiodyear.setYearquarter(year+"-0"+i);
			cwBdAccperiodyear.setUseState("1");
			cwBdAccperiodyear.setCreateTime(new Date());
			cwBdAccperiodyear.setUpdateTime(new Date());
			cwBdAccperiodyearMapper.insert(cwBdAccperiodyear);
		}
		//会计月份
		for(int i =1;i<=12;i++) {
			cwBdAccperiodmonth.setPkAccperiodmonth(SnowflakeIdWorker.getInstance().nextIdString());
			cwBdAccperiodmonth.setPkAccperiod(pk);
			cwBdAccperiodmonth.setAccperiodmth(((i>9)?"":"0")+i);
			String begin=year+"-"+((i>9)?"":"0")+i+"-01";
			try {
				cwBdAccperiodmonth.setBegindate(sdf.parse(begin));
			} catch (ParseException e1) {
				Shift.fatalThrow(SysStatusCode.INVALID_MODEL_FIELDS);
			}
			String ending=year+"-"+((i>9)?"":"0")+i+"-"+LastAndBeginYear.getDayOfMonth(Integer.parseInt(year), i);
			try {
				cwBdAccperiodmonth.setEnddate(sdf.parse(ending));
			} catch (ParseException e) {
				Shift.fatalThrow(SysStatusCode.INVALID_MODEL_FIELDS);
			}
			cwBdAccperiodmonth.setYearmth(year+"-"+((i>9)?"":"0")+i);
			cwBdAccperiodmonth.setUseState("1");
			cwBdAccperiodmonth.setCreateTime(new Date());
			cwBdAccperiodmonth.setUpdateTime(new Date());
			res=cwBdAccperiodmonthMapper.insert(cwBdAccperiodmonth);
		}
		if (res > 0) {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysConstants.DEFAULT_MESSAGE.OPERATION_SUCCESS,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
		}else {
			return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.FAIL,SysStatusCode.OPERATION_FAIL.message(),MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_FAIL));
		}
	}

}
