package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.SsmAccountTradeRecordDao;
import com.ssm.manage.entity.AccountTradeRecord;
import com.ssm.manage.entity.BaseZengka;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.mapper.AccountTradeRecordMapper;
import com.ssm.manage.query.AccountTradeRecordRequest;
import com.ssm.manage.response.AccountTradeRecordResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmAccountTradeRecordService;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import tk.mybatis.mapper.entity.Example;

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

@Service
@Slf4j
public class SsmAccountTradeRecordServiceImpl extends BaseServiceImpl<AccountTradeRecord> implements SsmAccountTradeRecordService {
    @Autowired
    private SsmAccountTradeRecordDao dao;

    @Override
    public SsmAccountTradeRecordDao getDao() {
        return dao;
    }

    @Override
    public void createTable() {
        String date = DateUtils.YMFormat(DateUtils.diffMonth(new Date(),1));//获取下个月yyyyMM时间格式
        dao.createTable(date);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int addRecord(AccountTradeRecord accountTradeRecord) throws SmsException {
        int i = 0;
        try {
            String phone=accountTradeRecord.getMemberPhone();
            accountTradeRecord.setMemberPhone(phone);
//            accountTradeRecord.setAccountGiftConsumeMoney(accountTradeRecord.getAccountGiftConsumeMoney().abs());
//            accountTradeRecord.setAccountGiftRestMoney();
            i = dao.addRecord(accountTradeRecord);
        }catch (Exception e){
            log.error("SsmAccountTradeRecordServiceImpl.addRecord:",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        return i;
    }
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int addRecord1(AccountTradeRecord accountTradeRecord) throws SmsException {
        int i = 0;
        try {
            String phone=accountTradeRecord.getMemberPhone();
            accountTradeRecord.setMemberPhone(phone);
            i = dao.addRecord1(accountTradeRecord);
        }catch (Exception e){
            log.error("SsmAccountTradeRecordServiceImpl.addRecord:",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        return i;
    }

    @Override
    public List<AccountTradeRecord> selectList(AccountTradeRecordRequest tradeRecordRequest) {
        List<AccountTradeRecord> list = dao.selectList(tradeRecordRequest);
        return list;
    }

    @Override
    public List<AccountTradeRecord> selectById(AccountTradeRecordRequest tradeRecordRequest) {
        List<AccountTradeRecord> list = dao.selectById(tradeRecordRequest);
        return list;
    }



    @Override
    public PageInfo<AccountTradeRecord> selectPage(AccountTradeRecordRequest tradeRecordRequest) {
        return dao.selectPage(tradeRecordRequest);
    }

    @Override
    public PageInfo<AccountTradeRecordResponse> selectPages(AccountTradeRecordRequest tradeRecordRequest) {
//        List<AccountTradeRecord> accountTradeRecords = new ArrayList<>();

        PageInfo<AccountTradeRecordResponse> accountTradeRecordPageInfo = null;
        try {
            accountTradeRecordPageInfo = dao.selectPages(tradeRecordRequest);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(tradeRecordRequest.getInputDate()); // 设置为当前时间
//        calendar.add(Calendar.MONTH, -1);
//        tradeRecordRequest.setInputDate(calendar.getTime());
//        PageInfo<AccountTradeRecord> accountTradeRecordPageInfo1 = dao.selectPages(tradeRecordRequest);
//        calendar.setTime(tradeRecordRequest.getInputDate());
//        calendar.add(Calendar.MONTH, -1);
//        tradeRecordRequest.setInputDate(calendar.getTime());
//        PageInfo<AccountTradeRecord> accountTradeRecordPageInfo2 = dao.selectPages(tradeRecordRequest);

        return accountTradeRecordPageInfo;
    }
    @Override
    public PageInfo<AccountTradeRecordResponse> selectPagesNew(AccountTradeRecordRequest tradeRecordRequest) {
        PageInfo<AccountTradeRecordResponse> accountTradeRecordPageInfo = dao.selectPagesNew(tradeRecordRequest);
        return accountTradeRecordPageInfo;
    }

    @Override
    public AccountTradeRecord retransmission(AccountTradeRecord accountTradeRecord) throws SmsException, ParseException {
        return dao.retransmission(accountTradeRecord);
    }
    @Autowired
    private AccountTradeRecordMapper mapper;
    @Override
    public List<AccountTradeRecord> selectListByMonthly(AccountTradeRecordRequest accountTradeRecordRequest) {
        return mapper.selectListByMonthly(accountTradeRecordRequest);
    }

    @Override
    public int updateMemberName(AccountTradeRecordRequest accountTradeRecordRequest) {
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        BeanUtils.copyProperties(accountTradeRecordRequest,accountTradeRecord);
        return mapper.updateByPrimaryKey(accountTradeRecord);
    }

    @Override
    public int insertBatch(List<AccountTradeRecord> list) {
        return dao.insertBatch(list);
    }
}
