package com.hai.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.security.UserCenter;
import com.hai.dao.*;
import com.hai.entity.*;
import com.hai.enum_type.ApiProductTypeEnum;
import com.hai.model.BsProductConfigModel;
import com.hai.model.UserInfoModel;
import com.hai.service.ApiProductService;
import com.hai.service.CommonService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
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.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("apiProductService")
public class ApiProductServiceImpl implements ApiProductService {

    @Resource
    private CommonService commonService;

    @Resource
    private ApiProductMapper apiProductMapper;

    @Resource
    private UserCenter userCenter;

    @Resource
    private ApiProductConfigMapper apiProductConfigMapper;

    @Resource
    private ApiStarbucksProductsMapper apiStarbucksProductsMapper;

    @Resource
    private BsProductConfigMapper productConfigMapper;

    @Resource
    private ApiCityMapper apiCityMapper;

    @Resource
    private ApiKfcStoresMapper apiKfcStoresMapper;

    @Override
    public void insertApiProduct(JSONObject object) {
        // 转换金额数组
        JSONArray priceArray = object.getJSONArray("prices");

        // api产品
        ApiProduct apiProduct;

        for (Object price : priceArray) {
            SecDictionary dictionary = commonService.mappingSysCode("RECHARGE_AMOUNT", String.valueOf(price));
            BigDecimal priceBd = new BigDecimal(dictionary.getCodeName());
            apiProduct = new ApiProduct();
            apiProduct.setRechargePrice(priceBd);
            apiProduct.setRechargeType(object.getInteger("rechargeType"));
            apiProduct.setPhoneRechargeType(object.getInteger("phoneRechargeType"));
            apiProduct.setOperatorType(object.getInteger("operatorType"));
            apiProduct.setRechargePlatform("-" + StringUtils.join(object.getJSONArray("rechargePlatform"), "-") + "-");
            apiProduct.setCreateTime(new Date());
            apiProduct.setUpdateTime(new Date());
            apiProduct.setStatus(102);
            apiProduct.setSort(1);
            apiProduct.setRechargeType(1);
            apiProduct.setOperatorName(object.getString("operatorName"));
            apiProduct.setOperatorId(object.getLong("operatorId"));
            apiProductMapper.insert(apiProduct);
        }

    }

    @Override
    public void updateApiProduct(ApiProduct apiProduct) {
        apiProductMapper.updateByPrimaryKeySelective(apiProduct);
    }

    @Override
    public List<ApiProduct> getListApiProduct(Map<String, Object> map) {
        ApiProductExample example = new ApiProductExample();
        ApiProductExample.Criteria criteria = example.createCriteria();


        if (MapUtils.getLong(map, "rechargePlatform") != null) {
            criteria.andRechargePlatformLike("%" + MapUtils.getLong(map, "rechargePlatform") + "%");
        }
        if (MapUtils.getLong(map, "rechargeType") != null) {
            criteria.andRechargeTypeEqualTo(MapUtils.getInteger(map, "rechargeType"));
        }
        if (MapUtils.getLong(map, "operatorType") != null) {
            criteria.andOperatorTypeEqualTo(MapUtils.getInteger(map, "operatorType"));
        }
        if (MapUtils.getLong(map, "phoneRechargeType") != null) {
            criteria.andPhoneRechargeTypeEqualTo(MapUtils.getInteger(map, "phoneRechargeType"));
        }
        if (MapUtils.getLong(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        } else {
            criteria.andStatusNotEqualTo(100);
        }

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

    @Override
    public ApiProduct findById(Long id) {
        return apiProductMapper.selectByPrimaryKey(id);
    }

    @Override
    public void configApiProduct(JSONObject object) {

        // 操作人
        UserInfoModel sessionModel = userCenter.getSessionModel(UserInfoModel.class);

        // 转换产品数组
        JSONArray productsArray = object.getJSONArray("productIds");
        ApiProductConfig apiProductConfig;
        ApiProduct apiProduct;
        for (Object productId : productsArray) {
            apiProduct = apiProductMapper.selectByPrimaryKey(Long.valueOf(productId.toString()));
            apiProductConfig = new ApiProductConfig();
            apiProductConfig.setProductId(Long.valueOf(productId.toString()));
            apiProductConfig.setCreateTime(new Date());
            apiProductConfig.setOperatorId(sessionModel.getSecUser().getId());
            apiProductConfig.setOperatorName(sessionModel.getSecUser().getUserName());
            apiProductConfig.setStatus(101);
            apiProductConfig.setDiscount(object.getBigDecimal("discount"));
            apiProductConfig.setDiscountPrice(apiProduct.getRechargePrice().multiply(object.getBigDecimal("discount")).divide(new BigDecimal(100)));
            apiProductConfig.setMerchantsId(object.getLong("merchantsId"));
            apiProductConfig.setUpdateTime(new Date());
            apiProductConfigMapper.insert(apiProductConfig);
        }
    }

    @Override
    public ApiProductConfig findProductConfigById(Long id) {
        return apiProductConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateConfigApiProduct(ApiProductConfig apiProductConfig) {
        apiProductConfigMapper.updateByPrimaryKey(apiProductConfig);
    }

    @Override
    public ApiProductConfig findMchProduct(Map<String, Object> map) {
        ApiProductConfigExample example = new ApiProductConfigExample();
        ApiProductConfigExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map , "merchantsId") != null) {
            criteria.andMerchantsIdEqualTo(MapUtils.getLong(map , "merchantsId"));
        }
        if (MapUtils.getLong(map , "productId") != null) {
            criteria.andProductIdEqualTo(MapUtils.getLong(map , "productId"));
        }
        if (MapUtils.getLong(map , "id") != null) {
            criteria.andIdEqualTo(MapUtils.getLong(map , "id"));
        }

        List<ApiProductConfig> list = apiProductConfigMapper.selectByExample(example);

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

    }

    @Override
    public void updateStarbucksProducts(ApiStarbucksProducts starbucksProducts) {
        apiStarbucksProductsMapper.updateByPrimaryKey(starbucksProducts);
    }

    @Override
    public void insertStarbucksProducts(ApiStarbucksProducts starbucksProducts) {
        apiStarbucksProductsMapper.insert(starbucksProducts);
    }

    @Override
    public ApiStarbucksProducts findStarbucksProductsByGoodsId(Long goodsId) {

        ApiStarbucksProductsExample example = new ApiStarbucksProductsExample();

        example.createCriteria().andGoodsIdEqualTo(goodsId);

        List<ApiStarbucksProducts> list = apiStarbucksProductsMapper.selectByExample(example);

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

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void ProductConfig(JSONObject object) {
        // 产品配置
        BsProductConfig productConfig;
        productConfig = new BsProductConfig();
        productConfig.setProductId(object.getLong("productType"));
        productConfig.setCompanyId(object.getLong("companyId"));
        productConfig.setCreateTime(new Date());
        productConfig.setDiscount(object.getBigDecimal("discount"));
        productConfig.setStatus(101);
        productConfig.setOperatorId(object.getLong("operatorId"));
        productConfig.setOperatorName(object.getString("operatorName"));
        productConfig.setUpdateTime(new Date());

        productConfig.setProductName(ApiProductTypeEnum.getNameByType(object.getInteger("productType")));
        productConfig.setProductType(object.getInteger("productType"));

        productConfigMapper.insert(productConfig);

        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", productConfig.getId());
        postObject.put("productType", 5);

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

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateProductConfig(JSONObject object) {
        BsProductConfig productConfig = productConfigMapper.selectByPrimaryKey(object.getLong("id"));

        productConfig.setProductId(object.getLong("productType"));
        productConfig.setCompanyId(object.getLong("companyId"));
        productConfig.setDiscount(object.getBigDecimal("discount"));
        productConfig.setStatus(101);
        productConfig.setOperatorId(object.getLong("operatorId"));
        productConfig.setOperatorName(object.getString("operatorName"));
        productConfig.setUpdateTime(new Date());

        productConfig.setProductName(ApiProductTypeEnum.getNameByType(object.getInteger("productType")));
        productConfig.setProductType(object.getInteger("productType"));

        productConfigMapper.updateByPrimaryKey(productConfig);


        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", productConfig.getId());
        postObject.put("productType", 5);

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

    @Override
    public ApiStarbucksProducts findStarbucksProducts(Long id) {
        return apiStarbucksProductsMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ApiStarbucksProducts> getStarbucksProducts(Map<String, Object> map) {


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

        if (MapUtils.getString(map, "name") != null) {
            criteria.andNameLike("%" + MapUtils.getString(map, "name") + "%");
        }
        if (MapUtils.getString(map, "categoryName") != null) {
            criteria.andCategoryNameEqualTo(MapUtils.getString(map, "categoryName"));
        }

        return apiStarbucksProductsMapper.selectByExample(example);
    }

    @Override
    public List<ApiStarbucksProducts> findStarbucksByCategoryName(String categoryName) {
        ApiStarbucksProductsExample example = new ApiStarbucksProductsExample();
        example.createCriteria().andCategoryNameEqualTo(categoryName);


        return apiStarbucksProductsMapper.selectByExample(example);
    }

    @Override
    public List<BsProductConfigModel> getProductConfigModel(Map<String, Object> map) {
        return null;
    }

    @Override
    public List<BsProductConfig> getProductConfig(Map<String, Object> map) {

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

        if (MapUtils.getLong(map, "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(map, "companyId"));
        }
        if (MapUtils.getInteger(map, "productType") != null) {
            criteria.andProductTypeEqualTo(MapUtils.getInteger(map, "productType"));
        }
        if (MapUtils.getString(map, "productName") != null) {
            criteria.andProductNameEqualTo(MapUtils.getString(map, "productName"));
        }

        return productConfigMapper.selectByExample(example);
    }

    @Override
    public BsProductConfig findProductConfig(Long id) {
        return productConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsProductConfig findProductConfigByGoodsId(Long productId) {
        BsProductConfigExample example = new BsProductConfigExample();
        example.createCriteria().andProductIdEqualTo(productId);

        if (productConfigMapper.selectByExample(example).size() != 0) {
           return productConfigMapper.selectByExample(example).get(0);
        }

        return null;
    }

    @Override
    public List<String> getCategoryName() {
        return apiStarbucksProductsMapper.getCategoryName();
    }

    @Override
    public List<ApiCity> getAllCity(String cityName) {
        ApiCityExample example = new ApiCityExample();
        if (StringUtils.isNotBlank(cityName)) {
            example.createCriteria().andCityNameLike("%" + cityName + "%");
        }
        return apiCityMapper.selectByExample(example);
    }

    @Override
    public void insertCity(ApiCity apiCity) {
        apiCityMapper.insert(apiCity);
    }

    @Override
    public void updateCity(ApiCity apiCity) {
        apiCityMapper.updateByPrimaryKeySelective(apiCity);
    }

    @Override
    public void insertKfcStore(ApiKfcStores apiKfcStores) {
        apiKfcStoresMapper.insert(apiKfcStores);
    }

    @Override
    public void updateKfcStore(ApiKfcStores apiKfcStores) {
        apiKfcStoresMapper.updateByPrimaryKeySelective(apiKfcStores);
    }

    @Override
    public ApiKfcStores findKfcStores(Map<String, Object> map) {
        ApiKfcStoresExample example = new ApiKfcStoresExample();
        ApiKfcStoresExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(MapUtils.getString(map , "storeCode"))) {
            criteria.andStoreCodeEqualTo(MapUtils.getString(map , "storeCode"));
        }

        List<ApiKfcStores> apiKfcStores = apiKfcStoresMapper.selectByExample(example);

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

    @Override
    public List<ApiKfcStores> getKfcStoresList(Map<String, Object> map) {

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

        if (StringUtils.isNotBlank(MapUtils.getString(map , "storeName"))) {
            criteria.andStoreNameLike("%" + MapUtils.getString(map , "storeName") + "%");
        }
        if (StringUtils.isNotBlank(MapUtils.getString(map , "cityName"))) {
            criteria.andCityNameLike("%" + MapUtils.getString(map , "cityName") + "%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map , "storeCode"))) {
            criteria.andStoreCodeEqualTo(MapUtils.getString(map , "storeCode"));
        }
        if (StringUtils.isNotBlank(MapUtils.getString(map , "cityCode"))) {
            criteria.andCityCodeEqualTo(MapUtils.getString(map , "cityCode"));
        }

        return apiKfcStoresMapper.selectByExample(example);
    }
}
