package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.IdWorker;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxMerchant;
import com.iformall.domain.po.WxMerchantPowerBillConfig;
import com.iformall.domain.po.WxPowerBillAutoConfig;
import com.iformall.enums.EnumPowerBillBuildWay;
import com.iformall.mapper.WxMerchantMapper;
import com.iformall.mapper.WxMerchantPowerBillConfigMapper;
import com.iformall.mapper.WxPowerBillAutoConfigMapper;
import com.iformall.service.WxMerchantPowerBillConfigService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author gongbiao
 */
@Service
@Slf4j
public class WxMerchantPowerBillConfigServiceImpl implements WxMerchantPowerBillConfigService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxMerchantPowerBillConfigMapper wxMerchantPowerBillConfigMapper;

    @Autowired
    WxMerchantMapper wxMerchantMapper;

    @Autowired
    WxPowerBillAutoConfigMapper wxPowerBillAutoConfigMapper;



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

    @Override
    public WxMerchantPowerBillConfig getById(Long id) {
        return wxMerchantPowerBillConfigMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public ResultData saveOrUpdate(WxMerchantPowerBillConfig config) {

        Date date = new Date();
        if (config.getId() == null) {
            //按电表计费 从配置中拉取单价
            if (config.getBuildWay().equals(EnumPowerBillBuildWay.METER.getCode())) {
                WxPowerBillAutoConfig wxPowerBillAutoConfig = new WxPowerBillAutoConfig();
                wxPowerBillAutoConfig.setTenantId(config.getTenantId());
                WxPowerBillAutoConfig powerBillAutoConfig = wxPowerBillAutoConfigMapper.selectOne(new QueryWrapper(wxPowerBillAutoConfig));
                if (powerBillAutoConfig == null) {
                    return new ResultData(ErrorCode.POWER_BILL_CONFIGU_NOT_FOUND);
                }
                config.setPrice(powerBillAutoConfig.getUnitPrice());
            }
            WxMerchant wxMerchant = wxMerchantMapper.selectById(config.getMerchantId());
            if (wxMerchant == null) {
                return new ResultData(ErrorCode.MERCHANT_INFO_NOT_FOUND);
            }
            config.setMerchantName(wxMerchant.getName());
            config.setMerchantType(wxMerchant.getType());
            final IdWorker idWorker = IdWorker.get();
            config.setId(idWorker.nextId());
            config.setCreateDate(date);
            config.setUpdateDate(date);
            wxMerchantPowerBillConfigMapper.insert(config);
        } else {
            config.setUpdateDate(date);
            wxMerchantPowerBillConfigMapper.updateById(config);
        }
        return new ResultData();
    }

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


}
