package com.hai.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.utils.DateUtil;
import com.hai.dao.BsIntegralRebateMapper;
import com.hai.entity.BsIntegralRebate;
import com.hai.entity.BsIntegralRebateExample;
import com.hai.entity.HighOrder;
import com.hai.service.BsDistributionUserRelService;
import com.hai.service.BsIntegralRebateService;
import com.hai.service.HighOrderService;
import com.hai.service.HighUserService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("bsIntegralRebateService")
public class BsIntegralRebateServiceImpl implements BsIntegralRebateService {

    @Resource
    private BsIntegralRebateMapper bsIntegralRebateMapper;

    @Resource
    private HighUserService highUserService;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private BsDistributionUserRelService bsDistributionUserRelService;

    @Override
    public BsIntegralRebate findIntegralRebateByMap(Map<String, Object> map) {

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

        if (MapUtils.getInteger(map , "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(map , "type"));
        }
        if (MapUtils.getLong(map , "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(map , "companyId"));
        }
        if (MapUtils.getLong(map , "productId") != null) {
            criteria.andProductIdEqualTo(MapUtils.getLong(map , "productId"));
        }

        List<BsIntegralRebate> list = bsIntegralRebateMapper.selectByExample(example);

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

        return null;
    }

    @Override
    public BsIntegralRebate findIntegralRebateById(Long id) {
        return bsIntegralRebateMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BsIntegralRebate> getIntegralRebateByList(Map<String, Object> map) {
        BsIntegralRebateExample example = new BsIntegralRebateExample();
        BsIntegralRebateExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getInteger(map , "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(map , "type"));
        }
        if (MapUtils.getLong(map , "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(map , "companyId"));
        }
        if (MapUtils.getLong(map , "productId") != null) {
            criteria.andProductIdEqualTo(MapUtils.getLong(map , "productId"));
        }
        return bsIntegralRebateMapper.selectByExample(example);
    }

    @Override
    public void insertIntegralRebate(BsIntegralRebate bsIntegralRebate) {
        bsIntegralRebateMapper.insert(bsIntegralRebate);
    }

    @Override
    public void updateIntegralRebate(BsIntegralRebate bsIntegralRebate) {
        bsIntegralRebateMapper.updateByPrimaryKeySelective(bsIntegralRebate);
    }

    @Override
    public void deleteIntegralRebate(Long id) {
        bsIntegralRebateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void integralRebate(JSONObject object) {

        Map<String, Object> map = new HashMap<>();
        map.put("type" , object.getInteger("type"));
        map.put("productId" , object.getLong("productId"));
        map.put("companyId" , object.getLong("companyId"));

        if (object.getLong("companyId") != null) {

            BsIntegralRebate bsIntegralRebate = findIntegralRebateByMap(map);

            if (DateUtil.isEffectiveDate(object.getDate("createTime"), bsIntegralRebate.getStartTime() , bsIntegralRebate.getEndTime())) {

                HighOrder order = highOrderService.getOrderById(object.getLong("orderId"));

                order.setWhetherRebate(true);

                BigDecimal integralNum = object.getBigDecimal("price").multiply(bsIntegralRebate.getPercentage()).setScale( 0, BigDecimal.ROUND_HALF_UP );
                BigDecimal integralNumFirst = object.getBigDecimal("price").multiply(bsIntegralRebate.getFirstDistribution()).setScale( 0, BigDecimal.ROUND_HALF_UP );
                BigDecimal integralNumSecond = object.getBigDecimal("price").multiply(bsIntegralRebate.getSecondDistribution()).setScale( 0, BigDecimal.ROUND_HALF_UP );

                order.setWhetherRebate(true);

                JSONObject jsonObject = new JSONObject();

                jsonObject.put("integralNumFirst" , integralNumFirst);
                jsonObject.put("integralNumSecond" , integralNumSecond);
                jsonObject.put("title" , object.getString("name"));
                jsonObject.put("orderNo" , order.getOrderNo());
                jsonObject.put("userId" , object.getLong("userId"));
                jsonObject.put("logoImg" , object.getString("logoImg"));

                bsDistributionUserRelService.distributionRebate(jsonObject);
                highOrderService.updateOrder(order);
                highUserService.goldHandle(object.getLong("userId"), integralNum.intValue(), 1, 4, object.getLong("orderId") , object.getString("remark") + integralNum);


            }


        }

    }
}
