package com.hai.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hai.dao.OutRechargePriceMapper;
import com.hai.entity.*;
import com.hai.model.OutRechargePriceModel;
import com.hai.service.BsConfigService;
import com.hai.service.CommonService;
import com.hai.service.OutRechargePriceService;
import io.swagger.models.auth.In;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Service("outRechargePriceServiceImpl")
public class OutRechargePriceServiceImpl implements OutRechargePriceService {

    @Resource
    private OutRechargePriceMapper outRechargePriceMapper;

    @Resource
    private CommonService commonService;

    @Resource
    private BsConfigService bsConfigService;

    @Override
    public List<OutRechargePrice> getListRechargePrice(Map<String, Object> map) {
        OutRechargePriceExample example = new OutRechargePriceExample();
        OutRechargePriceExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(map, "companyId"));
        }

        if (MapUtils.getLong(map, "rechargePlatform") != null) {
            criteria.andRechargePlatformLike("%" + MapUtils.getLong(map, "rechargePlatform") + "%");
        }

        if (MapUtils.getLong(map, "operatorType") != null) {
            criteria.andOperatorTypeEqualTo(MapUtils.getInteger(map, "operatorType"));
        }
        if (MapUtils.getLong(map, "rechargeType") != null) {
            criteria.andRechargeTypeEqualTo(MapUtils.getInteger(map, "rechargeType"));
        }
        if (MapUtils.getLong(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        } else  {
            criteria.andStatusNotEqualTo(0);
        }
        if (MapUtils.getLong(map, "rechargeWay") != null) {
            criteria.andRechargeWayEqualTo(MapUtils.getInteger(map, "rechargeWay"));
        }

        example.setOrderByClause("sort asc");
        return outRechargePriceMapper.selectByExample(example);
    }

    @Override
    public List<OutRechargePrice> getRechargePriceByList(Map<String, Object> map) {

        return outRechargePriceMapper.getUserByPrice(map);
    }


    @Override
    public OutRechargePriceModel findById(Long id , Integer platformId) {

        OutRechargePrice outRechargePrice = outRechargePriceMapper.selectByPrimaryKey(id);

        OutRechargePriceModel outRechargePriceModel = new OutRechargePriceModel();
        BeanUtils.copyProperties(outRechargePrice, outRechargePriceModel);

        Map<String, Object> listMap = new HashMap<>();
        listMap.put("productType", "3");
        listMap.put("returnType", 1);
        listMap.put("sourceId", outRechargePrice.getId());
        listMap.put("platformId", platformId);

        outRechargePriceModel.setIntegralDiscount(bsConfigService.getProductDiscountByMap(listMap).getDiscount());
        outRechargePriceModel.setProductPayTypeString(bsConfigService.getProductPayTypeByMap(listMap));
        outRechargePriceModel.setProductPlatformString(bsConfigService.getProductPlatformByMap(listMap));
        return outRechargePriceModel;
    }

    @Override
    public OutRechargePrice findByGoodsId(Long goodsId) {

        OutRechargePriceExample example = new OutRechargePriceExample();
        OutRechargePriceExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(goodsId).andStatusEqualTo(1);

        List<OutRechargePrice> list = outRechargePriceMapper.selectByExample(example);

        if (list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insertRechargePrice(JSONObject object) {

        // 转换金额数组
        JSONArray priceArray = object.getJSONArray("rechargePrices");

        // 充值金额
        OutRechargePrice outRechargePrice;

        for (Object price : priceArray) {
            SecDictionary dictionary = commonService.mappingSysCode("RECHARGE_AMOUNT", String.valueOf(price));
            BigDecimal priceBd = new BigDecimal(dictionary.getCodeName());
            outRechargePrice = new OutRechargePrice();
            outRechargePrice.setRechargePrice(priceBd);
            outRechargePrice.setPayPrice(priceBd.multiply(object.getBigDecimal("discount").divide(new BigDecimal(100))));
            outRechargePrice.setDiscount(object.getBigDecimal("discount"));
            outRechargePrice.setRechargeType(object.getInteger("rechargeType"));
            outRechargePrice.setOperatorType(object.getInteger("operatorType"));
            outRechargePrice.setRechargePlatform("-" + StringUtils.join(object.getJSONArray("rechargePlatform"), "-") + "-");
            outRechargePrice.setRechargeWay(object.getInteger("rechargeWay"));
            outRechargePrice.setCompanyId(object.getLong("companyId"));
            outRechargePrice.setCreateTime(new Date());
            outRechargePrice.setUpdateTime(new Date());
            outRechargePrice.setStatus(3);
            outRechargePrice.setSort(1);
            outRechargePriceMapper.insert(outRechargePrice);

            JSONObject postObject = new JSONObject();
            postObject.put("payType", object.getJSONArray("payType"));
            postObject.put("integralDiscount", object.getBigDecimal("integralDiscount"));
            postObject.put("productPlatform", object.getJSONArray("productPlatform"));
            postObject.put("operatorId", object.getLong("operatorId"));
            postObject.put("operatorName", object.getString("operatorName"));
            postObject.put("sourceId", outRechargePrice.getId());
            postObject.put("productType", 3);

            // 配置支付方式
            commonService.configPayType(postObject);
            // 配置积分折扣比例
            commonService.configIntegralDiscount(postObject);
            // 配置展示平台
            commonService.configPlatform(postObject);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateRechargePrice(JSONObject object) {
        OutRechargePrice outRechargePrice = outRechargePriceMapper.selectByPrimaryKey(object.getLong("id"));
        outRechargePrice.setPayPrice(outRechargePrice.getRechargePrice().multiply(object.getBigDecimal("discount").divide(new BigDecimal(100))));
        outRechargePrice.setDiscount(object.getBigDecimal("discount"));
        outRechargePrice.setRechargeType(object.getInteger("rechargeType"));
        outRechargePrice.setOperatorType(object.getInteger("operatorType"));
        outRechargePrice.setRechargePlatform("-" + StringUtils.join(object.getJSONArray("rechargePlatform"), "-") + "-");
        outRechargePrice.setRechargeWay(object.getInteger("rechargeWay"));
        outRechargePrice.setCompanyId(object.getLong("companyId"));
        outRechargePrice.setCreateTime(new Date());
        outRechargePrice.setUpdateTime(new Date());
        outRechargePrice.setStatus(3);
        outRechargePrice.setSort(object.getInteger("sort"));
        outRechargePrice.setGoodsId(object.getLong("goodsId"));
        outRechargePriceMapper.updateByPrimaryKey(outRechargePrice);
        JSONObject postObject = new JSONObject();
        postObject.put("payType", object.getJSONArray("payType"));
        postObject.put("integralDiscount", object.getBigDecimal("integralDiscount"));
        postObject.put("productPlatform", object.getJSONArray("productPlatform"));
        postObject.put("operatorId", object.getLong("operatorId"));
        postObject.put("operatorName", object.getString("operatorName"));
        postObject.put("sourceId", outRechargePrice.getId());
        postObject.put("productType", 3);
        // 配置支付方式
        commonService.configPayType(postObject);
        // 配置积分折扣比例
        commonService.configIntegralDiscount(postObject);
        // 配置展示平台
        commonService.configPlatform(postObject);
    }

    @Override
    public void editPriceStatus(OutRechargePrice outRechargePrice) {
        outRechargePriceMapper.updateByPrimaryKey(outRechargePrice);
    }

    @Override
    public void deletePrice(Long id) {
        outRechargePriceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public JSONObject getProductList() {
        return null;
    }

}
