package com.neusoft.smart.pos.card.service;

import com.neusoft.smart.pos.card.constants.CardErrorCode;
import com.neusoft.smart.pos.card.dto.*;
import com.neusoft.smart.pos.card.persistent.domain.CardCapitalChange;
import com.neusoft.smart.pos.card.persistent.domain.CardCapitalChangeExample;
import com.neusoft.smart.pos.card.persistent.domain.CardCapitalChangeExt;
import com.neusoft.smart.pos.card.persistent.ext.domain.CardCapitalChangeQuery;
import com.neusoft.smart.pos.card.persistent.repository.CardCapitalChangeRepository;
import com.neusoft.smart.pos.dto.member.request.MemberCapitalChangeQueryRequest;
import com.neusoft.smart.pos.dto.member.response.MemberCapitalChangeResponse;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.framework.utils.BeanUtil;
import com.neusoft.smart.pos.framework.utils.DateUtils;
import lombok.val;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.List;

@Service
public class CardCapitalChangeService {
//    private final static Logger logger = LoggerFactory.getLogger(CardCapitalChangeService.class);

	@Autowired
	private CardCapitalChangeRepository cardCapitalChangeRepository;

    public PaginationExtendBean<CardCapitalChange> queryCardCapitalChangeList(CardCapitalChangeQueryRequest cardCapitalChangeQueryRequest) {
        //1.校验参数信息
        if(StringUtils.isEmpty(cardCapitalChangeQueryRequest.getTimeMode())){
            throw new BusinessCommonException(CardErrorCode.CAPITAL_CHANGE_STATISTICS_TIME_MODE_EMPTY);
        }
        if(!cardCapitalChangeQueryRequest.getTimeMode().equals("1") && !cardCapitalChangeQueryRequest.getTimeMode().equals("2")){
            throw new BusinessCommonException(CardErrorCode.CAPITAL_CHANGE_STATISTICS_TIME_MODE_ERROR);
        }
        if(cardCapitalChangeQueryRequest.getTimeMode().equals("1")){
            if(StringUtils.isEmpty(cardCapitalChangeQueryRequest.getStartTime()) && StringUtils.isEmpty(cardCapitalChangeQueryRequest.getEndTime())){

            }else if(StringUtils.isEmpty(cardCapitalChangeQueryRequest.getStartTime()) || StringUtils.isEmpty(cardCapitalChangeQueryRequest.getEndTime())){
                throw new BusinessCommonException(CardErrorCode.CAPITAL_CHANGE_STATISTICS_START_END_TIME_EMPTY);
            }
        }
        if(cardCapitalChangeQueryRequest.getTimeMode().equals("2")){
            if(StringUtils.isEmpty(cardCapitalChangeQueryRequest.getDateTime())){
                throw new BusinessCommonException(CardErrorCode.CAPITAL_CHANGE_STATISTICS_DATE_TIME_EMPTY);
            }
        }
        //2.处理查询时间
        String timeMode = cardCapitalChangeQueryRequest.getTimeMode();
        String startTime = cardCapitalChangeQueryRequest.getStartTime();
        String endTime = cardCapitalChangeQueryRequest.getEndTime();
        String dateTime = cardCapitalChangeQueryRequest.getDateTime();
        Date startTimeTemp = null;
        Date endTimeTemp = null;
        //时间模式(@1:按开始及结束时间查询@2:按年月查询)
        if(("1").equals(timeMode)){
            if(StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)){
                startTimeTemp = DateUtils.getDayStartTime(new Date());
                endTimeTemp = DateUtils.getDayEndTime(new Date());
            }else{
                if(startTime.length()>10){
                    startTimeTemp = DateUtils.parse(startTime,DateUtils.FORMAT_LONG);
                    endTimeTemp = DateUtils.parse(endTime,DateUtils.FORMAT_LONG);
                }else{
                    startTimeTemp = DateUtils.getDayStartTime(DateUtils.parse(startTime,DateUtils.FORMAT_SHORT));
                    endTimeTemp = DateUtils.getDayEndTime(DateUtils.parse(endTime,DateUtils.FORMAT_SHORT));
                }
            }
        }else if(("2").equals(timeMode)){
//            if(startTime.length()>10){
//                startTimeTemp = DateUtils.getDayStartTime(DateUtils.parse(dateTime,DateUtils.FORMAT_LONG));
//                endTimeTemp = DateUtils.getDayEndTime(DateUtils.parse(dateTime,DateUtils.FORMAT_LONG));
//            }else{
            startTimeTemp = DateUtils.getDayStartTime(DateUtils.getMonthFirstDay(DateUtils.parse(dateTime,DateUtils.FORMAT_SHORTER)));
            endTimeTemp = DateUtils.getDayEndTime(DateUtils.getMonthLastDay(DateUtils.parse(dateTime,DateUtils.FORMAT_SHORTER)));
//            }
        }

        val example = new CardCapitalChangeExample();
        example.createCriteria()
                .andCreatedTimeGreaterThan(startTimeTemp)
                .andCreatedTimeLessThanOrEqualTo(endTimeTemp);
        if(cardCapitalChangeQueryRequest.getCardNum() != null) {
            example.getOredCriteria().get(0).andCardNumEqualTo(cardCapitalChangeQueryRequest.getCardNum());
        }
        if(cardCapitalChangeQueryRequest.getChangeTypes() != null && cardCapitalChangeQueryRequest.getChangeTypes().size() > 0) {
            example.getOredCriteria().get(0).andChangeTypeIn(cardCapitalChangeQueryRequest.getChangeTypes());
        }
        example.setLimit(cardCapitalChangeQueryRequest.getLimit());
        example.setOffset(cardCapitalChangeQueryRequest.getOffset());

        val orderCount = cardCapitalChangeRepository.queryChangeCount(example);    //查询满足条件的总记录条数
        val changeList = cardCapitalChangeRepository.getList(example);

        PaginationExtendBean<CardCapitalChange> paginationExtendBean = new PaginationExtendBean<>();
        paginationExtendBean.setRows(changeList);
        paginationExtendBean.setTotal(orderCount);
        return paginationExtendBean;
    }
    

	public List<MemberCapitalChangeResponse> getList(MemberCapitalChangeQueryRequest memberCapitalChangeQueryRequest) {
    	CardCapitalChangeQuery cardCapitalChangeQuery = new CardCapitalChangeQuery();
    	BeanUtils.copyProperties(memberCapitalChangeQueryRequest, cardCapitalChangeQuery);
    	List<CardCapitalChangeExt> cccList = cardCapitalChangeRepository.getList(cardCapitalChangeQuery);
		return BeanUtil.parseList(cccList, MemberCapitalChangeResponse.class);
	}
	

	public Integer getCount(MemberCapitalChangeQueryRequest memberCapitalChangeQueryRequest) {
		CardCapitalChangeQuery cardCapitalChangeQuery = new CardCapitalChangeQuery();
    	BeanUtils.copyProperties(memberCapitalChangeQueryRequest, cardCapitalChangeQuery);
		return cardCapitalChangeRepository.getCount(cardCapitalChangeQuery);
	}

}
