package com.ssm.manage.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmAccountTradeRecordDao;
import com.ssm.manage.dao.SsmCashDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.mapper.AccountTradeRecordMapper;
import com.ssm.manage.query.AccountTradeRecordRequest;
import com.ssm.manage.query.CashListRequest;
import com.ssm.manage.query.RechargeListRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.AccountTradeRecordResponse;
import com.ssm.manage.response.RechargeAccountResponse;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class SsmAccountTradeRecordDaoImpl extends BaseDaoImpl<AccountTradeRecord> implements SsmAccountTradeRecordDao {
    @Autowired
    private AccountTradeRecordMapper mapper;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;
    @Autowired
    private SsmProjectService ssmProjectService;
    @Autowired
    private SsmRechargeTypeSevice ssmRechargeTypeSevice;
    @Autowired
    private SsmAccountTradeRecordService ssmAccountTradeRecordService;

    @Autowired
    private SsmCourseService ssmCourseService;

    @Autowired
    private SsmRepayService ssmRepayService;

    @Autowired
    private SsmRechargeService ssmRechargeService;

    @Autowired
    private SsmCashDao ssmCashDao;

    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;

    @Autowired
    private SsmCardChangeService ssmCardChangeService;

    @Autowired
    private SsmPayService ssmPayService;

    @Override
    public AccountTradeRecordMapper getMapper() {
        return mapper;
    }

    @Override
    public void createTable(String date) {
        mapper.createTable(date);
    }

    @Override
    public int addRecord(AccountTradeRecord accountTradeRecord) {
        return mapper.addRecord(accountTradeRecord);
    }
    @Override
    public int addRecord1(AccountTradeRecord accountTradeRecord) {
        return mapper.addRecord1(accountTradeRecord);
    }

    @Override
    public List<AccountTradeRecord> selectList(AccountTradeRecordRequest accountTradeRecordRequest) {
        return mapper.selectList(accountTradeRecordRequest);
    }

    @Override
    public List<AccountTradeRecord> selectById(AccountTradeRecordRequest accountTradeRecordRequest) {
        return mapper.selectById(accountTradeRecordRequest);
    }


    @Override
    public PageInfo<AccountTradeRecord> selectPage(AccountTradeRecordRequest accountTradeRecordRequest) {
//        PageHelper.startPage(accountTradeRecordRequest.getPageNum(), accountTradeRecordRequest.getPageSize(), accountTradeRecordRequest.getOrderAndSort());
        List<AccountTradeRecord> list = mapper.selectList(accountTradeRecordRequest);
        PageInfo<AccountTradeRecord> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<AccountTradeRecordResponse> selectPages(AccountTradeRecordRequest accountTradeRecordRequest) throws ParseException{
        StoreRequest stores = new StoreRequest();
        stores.setId(accountTradeRecordRequest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(stores);
        List<Long> listStore = new ArrayList<>();
        for (BaseStores aLong : pagesorganizations) {
            listStore.add(aLong.getId());
        }
        accountTradeRecordRequest.setStoreIdl(listStore);
        accountTradeRecordRequest.setMemberPhone(null);
        List<AccountTradeRecord> listTotal = mapper.selectLists(accountTradeRecordRequest);
        PageHelper.startPage(accountTradeRecordRequest.getPageNum(), accountTradeRecordRequest.getPageSize(), accountTradeRecordRequest.getOrderAndSort());
        List<AccountTradeRecord> list = mapper.selectLists(accountTradeRecordRequest);
        List<AccountTradeRecordResponse> lists =new ArrayList<>();
        for (AccountTradeRecord accountTradeRecord : list) {
            StoreRequest storeRequest = new StoreRequest();
            storeRequest.setId(accountTradeRecord.getStoreId());
            BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
            AccountTradeRecordResponse accountTradeRecordResponse = new AccountTradeRecordResponse();
            BeanUtils.copyProperties(accountTradeRecord,accountTradeRecordResponse);
            accountTradeRecordResponse.setStoreNum(baseStore.getStoreNum());


            if(null ==accountTradeRecord.getTradeMoney()){
                accountTradeRecord.setTradeMoney(BigDecimal.valueOf(0));
            }
            if(null ==accountTradeRecord.getTradeCount()){
                accountTradeRecord.setTradeCount(0);
            }
            if(accountTradeRecord.getTradeCount()!=0){
                accountTradeRecordResponse.setPrice(accountTradeRecord.getTradeMoney().divide(BigDecimal.valueOf(accountTradeRecord.getTradeCount()),3,BigDecimal.ROUND_HALF_UP));
            }
            if(null == accountTradeRecord.getOrderStatu()){
                accountTradeRecordResponse.setOrderStatu(0);
            }

            //充值
            RechargeType rechargeType1 = null;
            if(accountTradeRecordResponse.getTradeId()!=null){

                RechargeType rechargeType = new RechargeType();
                rechargeType.setId(accountTradeRecordResponse.getTradeId());
                rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
            }
            if(null != rechargeType1){
                if(null != rechargeType1){
                    accountTradeRecordResponse.setAccountNum(rechargeType1.getRechargeTypeNum());
                }
            }else{
                if(accountTradeRecordResponse.getTradeId()!=null) {
                    BaseProject baseProject = new BaseProject();
                    baseProject.setId(accountTradeRecordResponse.getTradeId());
                    BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                    if (null != baseProject1) {
                        accountTradeRecordResponse.setAccountNum(baseProject1.getProjectNum());
                    }
                }
            }

            if(accountTradeRecordResponse.getAccountGiftRestMoney()==null){
                accountTradeRecordResponse.setAccountGiftRestMoney(new BigDecimal("0"));
            }
            if(accountTradeRecordResponse.getAccountGiftConsumeCount()==null){
                accountTradeRecordResponse.setAccountGiftConsumeCount(0);
            }
            lists.add(accountTradeRecordResponse);
        }

        //没有显示补单日期，疗程开卡改为疗程开卡补单
        for (AccountTradeRecordResponse accountTradeRecordResponse : lists) {
            if ("card_use".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    Date date = accountTradeRecordResponse.getInputDate();
                    String format = DateUtils.YMFormat(date);
                    CourseList courseList = new CourseList();
                    Date parse = DateUtils.YMParse(format);
                    courseList.setDate(parse);
                    courseList.setId(accountTradeRecordResponse.getDocId());
                    CourseList one = null;
                    if(ssmCourseService.selectList(courseList)!=null &&ssmCourseService.selectList(courseList).size()>0){
                         one = ssmCourseService.selectList(courseList).get(0);
                    }
                    if(one==null&&accountTradeRecordResponse.getOrderStatu()==0){
                        courseList.setCourseReatmentNum(accountTradeRecordResponse.getDocNum());
                        if(ssmCourseService.selectList2(courseList)!=null &&ssmCourseService.selectList2(courseList).size()>0){
                            one = ssmCourseService.selectList2(courseList).get(0);
                        }
                    }
                    if (null != one && one.getIsAddition() == 1){
                        //补单日期
                        accountTradeRecordResponse.setAdditionDate(one.getAdditionDate());
                        accountTradeRecordResponse.setDocType("card_use_addition");
                    }
                }
            }else if ("card_recharge".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    RechargeList rechargeList = new RechargeList();
                    Date parse = simpleDateFormat.parse(DateUtils.ymdhmsFormat(accountTradeRecordResponse.getInputDate()));
                    rechargeList.setId(accountTradeRecordResponse.getDocId());
                    rechargeList.setRechargeDate(accountTradeRecordResponse.getInputDate());
                    rechargeList.setStoreId(accountTradeRecordResponse.getStoreId());
                    RechargeList one = ssmRechargeService.selectOne(rechargeList);
                    if (null != one && one.getIsAddition() == 1){
                        accountTradeRecordResponse.setAdditionDate(one.getAdditionDate());
                    }
                }
            }else if ("store_cash".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    CashListRequest cashList = new CashListRequest();
                    Date parse = simpleDateFormat.parse(DateUtils.ymdhmsFormat(accountTradeRecordResponse.getInputDate()));
                    cashList.setCashDate(parse);
                    cashList.setId(accountTradeRecordResponse.getDocId());
                    cashList.setStoreId(accountTradeRecordResponse.getStoreId());
                    PageInfo<CashList> pageInfo = ssmCashDao.selectCashListGroups(cashList);
                    if (pageInfo.getList().size() > 0){
                        CashList one = pageInfo.getList().get(0);
                        if (null != one && one.getIsAddition() == 1){
                            accountTradeRecordResponse.setAdditionDate(one.getAdditionDate());
                        }
                    }
                }
            }else if ("card_repay".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    RepayList one = ssmRepayService.selectByPrimaryKey(accountTradeRecordResponse.getDocId());
                    if (null != one && one.getIsAddition() == 1){
                        accountTradeRecordResponse.setAdditionDate(one.getAdditionDate());
                    }
                }
            }else if ("turn_card_recharge".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    //无
                }
            }else if ("turn_card_use".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    //无
                }
            }else if ("course_modify".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){

                }
            }else if ("recharge_modify".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){

                }
            }else if ("recharge_groupon_card".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    //无
                }
            }else if ("recharge_roll_course".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    //无
                }
            }else if ("cancel_Debt".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    //无
                }
            }else if ("promotion_card_repay".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    RepayList one = ssmRepayService.selectByPrimaryKey(accountTradeRecordResponse.getDocId());
                    if (null != one && one.getIsAddition() == 1){
                        accountTradeRecordResponse.setAdditionDate(one.getAdditionDate());
                    }
                }
            }else if ("card_change".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    CardChangeList one = ssmCardChangeService.selectByPrimaryKey(accountTradeRecordResponse.getDocId());
                    if (null != one && one.getIsAddition() == 1){
                        accountTradeRecordResponse.setAdditionDate(one.getAdditionDate());
                    }
                }
            }else if ("refund_recharge".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    //无
                }
            }else if ("refund_course".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    //无
                }
            }else if ("recharge_Transition".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    //无
                }
            }else if ("promotion".equals(accountTradeRecordResponse.getDocType())){
                if (null == accountTradeRecordResponse.getAdditionDate()){
                    //无
                }
            }
        }

        PageInfo<AccountTradeRecordResponse> pageInfo = new PageInfo<>(lists);
        pageInfo.setTotal(listTotal.size());
        return pageInfo;
    }

    @Override
    public PageInfo<AccountTradeRecordResponse> selectPagesNew(AccountTradeRecordRequest accountTradeRecordRequest) {
        StoreRequest stores = new StoreRequest();
        stores.setId(accountTradeRecordRequest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(stores);
        List<Long> listStore = new ArrayList<>();
        for (BaseStores aLong : pagesorganizations) {
            listStore.add(aLong.getId());
        }
        accountTradeRecordRequest.setStoreIdl(listStore);
        List<AccountTradeRecord> listTotal = mapper.selectListsNew(accountTradeRecordRequest);
        PageHelper.startPage(accountTradeRecordRequest.getPageNum(), accountTradeRecordRequest.getPageSize(), accountTradeRecordRequest.getOrderAndSort());
        List<AccountTradeRecord> list = mapper.selectListsNew(accountTradeRecordRequest);
        StoreRequest storeRequest = new StoreRequest();
        List<AccountTradeRecordResponse> lists =new ArrayList<>();
        for (AccountTradeRecord accountTradeRecord : list) {
            storeRequest.setId(accountTradeRecord.getStoreId());
            BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
            AccountTradeRecordResponse accountTradeRecordResponse = new AccountTradeRecordResponse();
            BeanUtils.copyProperties(accountTradeRecord,accountTradeRecordResponse);
            accountTradeRecordResponse.setStoreNum(baseStore.getStoreNum());
            if(null ==accountTradeRecord.getTradeMoney()){
                accountTradeRecord.setTradeMoney(BigDecimal.valueOf(0));
            }
            if(null ==accountTradeRecord.getTradeCount()){
                accountTradeRecord.setTradeCount(0);
            }
            if(accountTradeRecord.getTradeCount()!=0){
                accountTradeRecordResponse.setPrice(accountTradeRecord.getTradeMoney().divide(BigDecimal.valueOf(accountTradeRecord.getTradeCount()),3,BigDecimal.ROUND_HALF_UP));
            }
            if(null == accountTradeRecord.getOrderStatu()){
                accountTradeRecordResponse.setOrderStatu(0);
            }

            //充值
            RechargeType rechargeType = new RechargeType();
            rechargeType.setId(accountTradeRecordResponse.getTradeId());
            RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
            if(null != rechargeType1){
                if(null != rechargeType1){
                    accountTradeRecordResponse.setAccountNum(rechargeType1.getRechargeTypeNum());
                }
            }else{
                BaseProject baseProject = new BaseProject();
                baseProject.setId(accountTradeRecordResponse.getTradeId());
                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                if(null != baseProject1){
                    accountTradeRecordResponse.setAccountNum(baseProject1.getProjectNum());
                }
            }

            lists.add(accountTradeRecordResponse);
        }
        PageInfo<AccountTradeRecordResponse> pageInfo = new PageInfo<>(lists);
        pageInfo.setTotal(listTotal.size());
        return pageInfo;
    }

    /*
     * 充值卡付列表设置返回值
     * */
    public List<RechargeAccountResponse> setRechargeAccountList(CourseList courseList){
        List<RechargeAccountResponse> list = new ArrayList<>();
        AccountTradeRecordRequest accountTradeRecordRequest = new AccountTradeRecordRequest();
        accountTradeRecordRequest.setDocType(Constant.doc_type_card_use);
        accountTradeRecordRequest.setAccountType(Constant.account_type_recharge_consume);
        accountTradeRecordRequest.setDocId(courseList.getId());
        accountTradeRecordRequest.setInputDate(courseList.getDate());
        List<AccountTradeRecord> recordList = ssmAccountTradeRecordService.selectList(accountTradeRecordRequest);
        for (AccountTradeRecord accountTradeRecord : recordList) {
            RechargeAccountResponse rechargeAccountResponse = new RechargeAccountResponse();
            rechargeAccountResponse.setCardRestMoney(accountTradeRecord.getAccountRestMoney());
            rechargeAccountResponse.setCardConsumeMoney(accountTradeRecord.getAccountConsumeMoney());
            rechargeAccountResponse.setCardGiftMoney(accountTradeRecord.getAccountGiftRestMoney());
            rechargeAccountResponse.setGiftConsumeMoney(accountTradeRecord.getAccountGiftConsumeMoney());
            rechargeAccountResponse.setSpendMoney(accountTradeRecord.getAccountSpendMoney());
            rechargeAccountResponse.setRechargeTypeName(accountTradeRecord.getAccountName());
            list.add(rechargeAccountResponse);
        }
        return list;
    }

    @Override
    public AccountTradeRecord retransmission(AccountTradeRecord accountTradeRecord) throws SmsException, ParseException {
//        Date date = new Date();
        Date date = accountTradeRecord.getInputDate();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
        String time = simpleDateFormat.format(date);
        List<AccountTradeRecord> list=mapper.retransmission(accountTradeRecord.getDocId(),time);
        AccountTradeRecord accountTradeRecord1=new AccountTradeRecord();
        accountTradeRecord1=list.get(0);
        return accountTradeRecord1;
    }

    @Override
    public int insertBatch(List<AccountTradeRecord> list) {
        Map<String,Object> map = new HashMap<>();
        map.put("tab", DateUtils.YMFormat(new Date()));
        map.put("list",list);
        return mapper.insertBatch(map);
    }
}
