package com.iformall.service.impl;

import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.common.ResultData;
import com.iformall.domain.po.MallUserInfo;
import com.iformall.domain.po.WxBillSettle;
import com.iformall.domain.po.WxBillSettleRecord;
import com.iformall.domain.po.WxMerchantBUser;
import com.iformall.enums.EnumSettleRecordWay;
import com.iformall.enums.EnumSettleStatus;
import com.iformall.enums.EnumSettleWay;
import com.iformall.mapper.WxBillSettleMapper;
import com.iformall.mapper.WxBillSettleRecordMapper;
import com.iformall.mapper.WxMerchantBUserMapper;
import com.iformall.service.WxBillSettleRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author luozukai
 * @date 2019/8/26 17:37
 */
@Service
public class WxBillSettleRecordServiceImpl implements WxBillSettleRecordService {
    @Autowired
    private WxBillSettleRecordMapper wxBillSettleRecordMapper;
    @Autowired
    private WxBillSettleMapper wxBillSettleMapper;
    @Autowired
    private WxMerchantBUserMapper wxMerchantBUserMapper;

    @Override
    public PageInfo<WxBillSettleRecord> listAsPage(WxBillSettleRecord record, Integer pageIndex, Integer pageSize) {
        return null;
    }

    @Override
    public ResultData saveOrUpdate(WxBillSettleRecord record,MallUserInfo mallUserInfo,Long bUserId) {
        final IdWorker idWorker = IdWorker.get();
        if (record.getId() == null) {
            record.setId(idWorker.nextId());

            //清算完成
            if(record.getSettleMoney()!= null && record.getBalance()!=null && record.getSettleMoney().intValue() == abs(record.getBalance().intValue())){
                WxBillSettle wxBillSettle = new WxBillSettle();
                wxBillSettle.setId(record.getSettleId());
                wxBillSettle.setStatus(EnumSettleStatus.FINISH.getCode());
                wxBillSettleMapper.updateById(wxBillSettle);
            }
            if(record.getBalance()!=null && record.getBalance() < 0){
                record.setSettleMoney(-record.getSettleMoney());
            }

            String str;
            if(EnumSettleRecordWay.OTHER.getCode().equals(record.getSettleWay())){
                str = record.getRemark()+"-收款"+new BigDecimal(record.getSettleMoney()).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP)+"元";
            }else{
                str = EnumSettleRecordWay.getEnum(record.getSettleWay()).getMessage()+"收款"+new BigDecimal(record.getSettleMoney()).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP)+"元";
            }
            record.setDetail(str);
            if(mallUserInfo != null) {
                record.setUserid(mallUserInfo.getId());
                record.setUserName(mallUserInfo.getName());
            }else if(bUserId != null){
                WxMerchantBUser wxMerchantBUser = wxMerchantBUserMapper.selectById(bUserId);
                if(wxMerchantBUser!=null)
                    record.setUserName(wxMerchantBUser.getName());
            }
            wxBillSettleRecordMapper.insert(record);
        }else{
            wxBillSettleRecordMapper.updateById(record);
        }
        return new ResultData();
    }

    public static Integer abs(Integer a){
        return a>0?a:-a;
    }



}
