package com.hai.openApi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.dao.ApiMchProductMapper;
import com.hai.entity.ApiMchProduct;
import com.hai.entity.ApiMchProductExample;
import com.hai.entity.SecConfig;
import com.hai.openApi.service.ApiMchProductService;
import com.hai.order.type.OrderProductType;
import com.hai.service.SecConfigService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service("apiMchProductService")
public class ApiMchProductServiceImpl implements ApiMchProductService {

    @Resource
    private ApiMchProductMapper apiMchProductMapper;

    @Resource
    private SecConfigService secConfigService;

    @Override
    public void insertMchProduct(ApiMchProduct apiMchProduct) {
        apiMchProductMapper.insert(apiMchProduct);
    }

    @Override
    public void configMchProduct(JSONObject object) {

        // 转换产品类型数组
        JSONArray productTypeS = object.getJSONArray("productType");

        ApiMchProduct apiMchProduct;

        for (Object productType : productTypeS) {

            apiMchProduct = new ApiMchProduct();

            apiMchProduct.setProductType(Integer.valueOf(productType.toString()));
            apiMchProduct.setMchId(object.getString("mchId"));
            apiMchProduct.setDiscount(object.getBigDecimal("discount"));
            apiMchProduct.setStatus(1);
            apiMchProduct.setCreateTime(new Date());
            apiMchProduct.setUpdateTime(new Date());


            if (Integer.valueOf(productType.toString()).equals(OrderProductType.PRODUCT_TYPE6.getNumber())) {
                if (object.getLong("companyId") == null ||
                        object.getString("merchantId") == null
                ) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                JSONArray merchantIdS = object.getJSONArray("merchantId");
                for (Object merchantId : merchantIdS) {

                    if (whereExistence((object.getString("mchId")) , Integer.valueOf(productType.toString()) , Long.valueOf(merchantId.toString()))) {
                        continue;
                    }

                    apiMchProduct.setMerchantId(Long.valueOf(merchantId.toString()));
                    apiMchProduct.setCompanyId(object.getLong("companyId"));
                    insertMchProduct(apiMchProduct);
                }
            } else {

                insertMchProduct(apiMchProduct);
            }

            if (secConfigService.isConfig(OrderProductType.getDataByType(Integer.valueOf(productType.toString())).getApiCode() , object.getString("mchId"))) {
                continue;
            }

            // 插入接口权限
            SecConfig secConfig = new SecConfig();
            secConfig.setCodeValue(object.getString("mchId"));
            secConfig.setCodeType(OrderProductType.getDataByType(Integer.valueOf(productType.toString())).getApiCode());
            secConfigService.insertSecConfig(secConfig);

        }

    }

    @Override
    public void updateMchProduct(ApiMchProduct apiMchProduct) {
        apiMchProductMapper.updateByPrimaryKey(apiMchProduct);
    }

    @Override
    public List<ApiMchProduct> getListMchProduct(Map<String, Object> map) {
        ApiMchProductExample example = new ApiMchProductExample();
        ApiMchProductExample.Criteria criteria  = example.createCriteria();

        if (MapUtils.getString(map , "mchId") != null) {
            criteria.andMchIdEqualTo(MapUtils.getString(map , "mchId"));
        }
        if (MapUtils.getInteger(map , "productType") != null) {
            criteria.andProductTypeEqualTo(MapUtils.getInteger(map , "productType"));
        }

        criteria.andStatusEqualTo(1);
        return apiMchProductMapper.selectByExample(example);
    }

    @Override
    public ApiMchProduct findById(Long id) {
        return apiMchProductMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean whereExistence(String mchId, Integer productType , Long merchantId) {
        ApiMchProductExample example = new ApiMchProductExample();

        example.createCriteria().andMchIdEqualTo(mchId).andProductTypeEqualTo(productType).andMerchantIdEqualTo(merchantId);

        return apiMchProductMapper.selectByExample(example).size() > 0;
    }

    @Override
    public ApiMchProduct findByMap(Map<String, Object> map) {
        ApiMchProductExample example = new ApiMchProductExample();
        ApiMchProductExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getInteger(map , "productType") != null) {
            criteria.andProductTypeEqualTo(MapUtils.getInteger(map , "productType"));
        }
        if (StringUtils.isNotBlank(MapUtils.getString(map, "mchId"))) {
            criteria.andMchIdEqualTo(MapUtils.getString(map, "mchId"));
        }
        if (MapUtils.getLong(map, "merchantId") != null) {
            criteria.andMerchantIdEqualTo(MapUtils.getLong(map, "merchantId"));
        }

        List<ApiMchProduct> list = apiMchProductMapper.selectByExample(example);

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

        return null;
    }

    @Override
    public void detailConfigMchProduct(String mchId) {
        ApiMchProductExample example = new ApiMchProductExample();
        example.createCriteria().andMchIdEqualTo(mchId);

        List<ApiMchProduct> list = apiMchProductMapper.selectByExample(example);
        for (ApiMchProduct apiMchProduct : list) {
            apiMchProductMapper.deleteByPrimaryKey(apiMchProduct.getId());
        }

    }

    @Override
    public void detailApiMchProduct(Long id) {
        apiMchProductMapper.deleteByPrimaryKey(id);
    }
}
