package com.iformall.service.impl;

import java.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.domain.po.WxDateAmountRecord;
import com.iformall.mapper.WxDateAmountRecordMapper;
import com.iformall.service.WxDateAmountRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iformall.common.IdWorker;

@Service
public class WxDateAmountRecordServiceImpl implements WxDateAmountRecordService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxDateAmountRecordMapper wxDateAmountRecordMapper;


    @Override
    public PageInfo<WxDateAmountRecord> listAsPage(WxDateAmountRecord record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxDateAmountRecordMapper.findList(record));
    }

    @Override
    public WxDateAmountRecord getById(Long id) {
        return wxDateAmountRecordMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(WxDateAmountRecord record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            wxDateAmountRecordMapper.insert(record);
        } else {
            wxDateAmountRecordMapper.updateById(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        wxDateAmountRecordMapper.deleteById(id);
    }


    @Override
    public void saveAmount(WxDateAmountRecord amount) {

        WxDateAmountRecord record  = new WxDateAmountRecord();
        record.setDate(amount.getDate());
        record.setTenantId(amount.getTenantId());
        record.setMerchantId(amount.getMerchantId());
        record.setType(amount.getType());

        List<WxDateAmountRecord> list = wxDateAmountRecordMapper.findList(record);

        if (list.size() > 0){
            list.get(0).setPayPrice(amount.getPayPrice());
            list.get(0).setUpdateDate(new Date());
            wxDateAmountRecordMapper.updateById(list.get(0));
        }else{
            final IdWorker idWorker = IdWorker.get();
            amount.setId(idWorker.nextId());
            amount.setUpdateDate(new Date());
            amount.setCreateDate(new Date());
            wxDateAmountRecordMapper.insert(amount);
        }
    }
}
