package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmStoreMonthTaskDao;
import com.ssm.manage.entity.AccountTradeRecord;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StoreMonthTask;
import com.ssm.manage.query.AccountTradeRecordRequest;
import com.ssm.manage.query.StoreMonthTaskRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StoreMonthTaskResponse;
import com.ssm.manage.service.SsmAccountTradeRecordService;
import com.ssm.manage.service.SsmStoreMonthTaskService;
import com.ssm.manage.utils.BigDecimalUtils;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class SsmStoreMonthTaskServiceImpl extends BaseServiceImpl<StoreMonthTask> implements SsmStoreMonthTaskService {
    @Autowired
    private SsmAccountTradeRecordService ssmAccountTradeRecordService;

    @Autowired
    private SsmStoreMonthTaskDao dao;

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

    @Override
    public PageInfo<StoreMonthTask> selectPage(StoreMonthTaskRequest storeMonthTaskRequest) {
        return dao.selectPage(storeMonthTaskRequest);
    }

    @Override
    public int addTask(StoreMonthTaskRequest storeMonthTaskRequest) throws ParseException {
        StoreMonthTask storeMonthTask = new StoreMonthTask();
        BeanUtils.copyProperties(storeMonthTaskRequest, storeMonthTask);
        storeMonthTask.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        storeMonthTask.setState(0);
        storeMonthTask.setDate(DateUtils.ymdString2Date(DateUtils.ymdFormat(DateUtils.ymParse(storeMonthTaskRequest.getDate()))));
        return dao.saveNotNull(storeMonthTask);
    }

    @Override
    public int updateTask(StoreMonthTaskRequest storeMonthTaskRequest) throws SmsException, ParseException {
        StoreMonthTask storeMonthTask = new StoreMonthTask();
        BeanUtils.copyProperties(storeMonthTaskRequest, storeMonthTask);
        if (storeMonthTaskRequest.getState() == 1){
            throw new SmsException(ResponseCode.MSG,"任务已经启用，禁止修改");
        }
        storeMonthTask.setDate(DateUtils.ymParse(storeMonthTaskRequest.getDate()));
        return dao.updateByPrimaryKeySelective(storeMonthTask);
    }

    @Override
    public int deleteTask(StoreMonthTaskRequest storeMonthTaskRequest) throws SmsException {
        StoreMonthTask storeMonthTask = dao.selectByPrimaryKey(storeMonthTaskRequest.getId());
        if (storeMonthTask.getState() == 1){
            throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATE);
        }
        return dao.deleteByPrimaryKey(storeMonthTaskRequest.getId());
    }

    @Override
    public int updateState(StoreMonthTaskRequest storeMonthTaskRequest) {
        StoreMonthTask storeMonthTask = new StoreMonthTask();
        BeanUtils.copyProperties(storeMonthTaskRequest, storeMonthTask);
//        storeMonthTask.setState(1);
        return dao.updateByPrimaryKeySelective(storeMonthTask);
    }

    @Override
    public StoreMonthTaskResponse statistic(StoreMonthTaskRequest storeMonthTaskRequest) throws SmsException, ParseException {
//        StoreMonthTask storeMonthTask = new StoreMonthTask();
//        BeanUtils.copyProperties(storeMonthTaskRequest, storeMonthTask);
//        storeMonthTask.setDate(DateUtils.ymdString2Date(storeMonthTaskRequest.getDate() + "-01"));
//        storeMonthTask = dao.selectOne(storeMonthTask);
        Example example = new Example(StoreMonthTask.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId", storeMonthTaskRequest.getStoreId());
        criteria.andLike("date",storeMonthTaskRequest.getDate() + "%");
        List<StoreMonthTask> storeMonthTaskList = dao.selectByExample(example);
        if(0 == storeMonthTaskList.size()){
            throw new SmsException(ResponseCode.DATA_DOES_NOT_EXISTS_S,"月度计划任务");
        }
        StoreMonthTask storeMonthTask = storeMonthTaskList.get(0);
        if (null == storeMonthTask){
            throw new SmsException(ResponseCode.DATA_DOES_NOT_EXISTS_S,"月度计划任务");
        }
        StoreMonthTaskResponse storeMonthTaskResponse = new StoreMonthTaskResponse();
        BeanUtils.copyProperties(storeMonthTask, storeMonthTaskResponse);
        //查询当月账户交易记录
        String dateStr = DateUtils.ymFormat(storeMonthTask.getDate());
        AccountTradeRecordRequest accountTradeRecordRequest = new AccountTradeRecordRequest();
        accountTradeRecordRequest.setDate(dateStr);
        accountTradeRecordRequest.setInputDate(storeMonthTask.getDate());
        accountTradeRecordRequest.setStoreId(storeMonthTask.getStoreId());
        List<AccountTradeRecord> accountTradeRecords = ssmAccountTradeRecordService.selectList(accountTradeRecordRequest);
        BigDecimal rechargeMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal courseMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal recRepayMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal courseRepayMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal recStoreCashMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal courseStoreCashMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal cashStoreCashMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal recCourseMoney = BigDecimalUtils.bigDecimal("0");
        for (AccountTradeRecord accountTradeRecord : accountTradeRecords) {
            if (Constant.doc_type_recharge.equals(accountTradeRecord.getDocType())){
                if (Constant.account_type_recharge.equals(accountTradeRecord.getAccountType())) {
                    if(null != accountTradeRecord.getAccountInMoney()){
                        rechargeMoney = BigDecimalUtils.add(rechargeMoney, accountTradeRecord.getAccountInMoney());
                    }
                }else if (Constant.account_type_recharge_cancel.equals(accountTradeRecord.getAccountType())){
                    if(null != accountTradeRecord.getAccountInMoney()){
                        rechargeMoney = BigDecimalUtils.subtract(rechargeMoney, accountTradeRecord.getAccountInMoney());
                    }
                }
            }else if (Constant.doc_type_card_use.equals(accountTradeRecord.getDocType())){
                if (Constant.account_type_project.equals(accountTradeRecord.getAccountType())) {
                    if(null != accountTradeRecord.getAccountInMoney()){
                        courseMoney = BigDecimalUtils.add(courseMoney, accountTradeRecord.getAccountInMoney());
                    }
                }else if (Constant.account_type_project_cancel.equals(accountTradeRecord.getAccountType())){
                    if(null !=  accountTradeRecord.getAccountInMoney()){
                        courseMoney = BigDecimalUtils.subtract(courseMoney, accountTradeRecord.getAccountInMoney());
                    }
                }else if (Constant.account_type_recharge_consume.equals(accountTradeRecord.getAccountType())) {
                    if(null !=   accountTradeRecord.getAccountSpendMoney()){
                        recCourseMoney = BigDecimalUtils.add(recCourseMoney, accountTradeRecord.getAccountSpendMoney());
                    }
                }else if (Constant.account_type_recharge_consume_cancel.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountInMoney()){
                        recCourseMoney = BigDecimalUtils.subtract(recCourseMoney, accountTradeRecord.getAccountInMoney());
                    }
                }
            }else if (Constant.doc_type_repay.equals(accountTradeRecord.getDocType())){
                if (Constant.account_type_rec_repay.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountInMoney()){
                        recRepayMoney = BigDecimalUtils.add(recRepayMoney, accountTradeRecord.getAccountInMoney());
                    }
                }else if (Constant.account_type_rec_repay_cancel.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountInMoney()){
                        recRepayMoney = BigDecimalUtils.subtract(recRepayMoney, accountTradeRecord.getAccountInMoney());
                    }
                }else if (Constant.account_type_course_repay.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountInMoney()){
                        courseRepayMoney = BigDecimalUtils.add(courseRepayMoney, accountTradeRecord.getAccountInMoney());
                    }
                }else if (Constant.account_type_course_repay_cancel.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountInMoney()){
                        courseRepayMoney = BigDecimalUtils.subtract(courseRepayMoney, accountTradeRecord.getAccountInMoney());
                    }
                }
            }else if (Constant.doc_type_store_cash.equals(accountTradeRecord.getDocType())){
                if (Constant.account_type_recharge_consume.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountSpendMoney()){
                        recStoreCashMoney = BigDecimalUtils.add(recStoreCashMoney, accountTradeRecord.getAccountSpendMoney());
                    }
                }else if (Constant.account_type_recharge_consume_cancel.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountInMoney()){
                        recStoreCashMoney = BigDecimalUtils.subtract(recStoreCashMoney, accountTradeRecord.getAccountInMoney());
                    }
                }else if (Constant.account_type_project_consume.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountSpendMoney()){
                        courseStoreCashMoney = BigDecimalUtils.add(courseStoreCashMoney, accountTradeRecord.getAccountSpendMoney());
                    }
                }else if (Constant.account_type_project_consume_cancel.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountInMoney()){
                        courseStoreCashMoney = BigDecimalUtils.subtract(courseStoreCashMoney, accountTradeRecord.getAccountInMoney());
                    }
                }else if (Constant.account_type_store_cash.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountSpendMoney()){
                        cashStoreCashMoney = BigDecimalUtils.add(cashStoreCashMoney, accountTradeRecord.getAccountSpendMoney());
                    }
                }else if (Constant.account_type_store_cash_cancel.equals(accountTradeRecord.getAccountType())){
                    if(null !=   accountTradeRecord.getAccountSpendMoney()){
                        cashStoreCashMoney = BigDecimalUtils.subtract(cashStoreCashMoney, accountTradeRecord.getAccountSpendMoney());
                    }
                }
            }
        }
        storeMonthTaskResponse.setRechargeMoney(rechargeMoney);
        storeMonthTaskResponse.setRecRepayMoney(recRepayMoney);
        storeMonthTaskResponse.setCourseMoney(courseMoney);
        storeMonthTaskResponse.setCourseRepayMoney(courseRepayMoney);
        storeMonthTaskResponse.setRecStoreCashMoney(recStoreCashMoney);
        storeMonthTaskResponse.setCourseStoreCashMoney(courseStoreCashMoney);
        storeMonthTaskResponse.setCashStoreCashMoney(cashStoreCashMoney);
        storeMonthTaskResponse.setRecCourseMoney(recCourseMoney);
        return storeMonthTaskResponse;
    }

}
