package com.org.hktshop.controller.portal.product;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.org.hktshop.Interface.product.IHktPositiveEnergyService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.entity.product.HktGoodItem;
import com.org.hktshop.entity.product.HktPositivePrice;
import com.org.hktshop.utils.HttpCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hotking03
 */
@RequestMapping("/portal/positive")
@RestController
public class PortalPositiveEnergyController  extends BaseController {


    @Autowired
    private IHktPositiveEnergyService hktPositiveEnergyService;

    @ApiOperation(value = "分页正能量列表", notes = "分页正能量列表", httpMethod = "POST")
    @RequestMapping(value = "/queryList",method = RequestMethod.POST)
    public Object queryPositiveByPage(@RequestBody HktGoodItem hktGoodItem) {
        try {
            if(hktGoodItem.getMaterialSn().length() < 3){
                return BaseResult.fail(HttpCode.BAD_REQUEST.getCode(),"物料编码长度不能小于三位");
            }
            JSONObject positiveProduct = hktPositiveEnergyService.getPositiveProduct(hktGoodItem);

            return BaseResult.successWithData(getPositiveProduct(positiveProduct,hktGoodItem.getMaterialSn()));
        } catch (Exception e) {
            logger.info("分页查询价格规则列表错误  error = {}",e.toString());
            e.printStackTrace();
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


    @ApiOperation(value = "分页正能量列表", notes = "分页正能量列表", httpMethod = "POST")
    @RequestMapping(value = "/queryList1",method = RequestMethod.POST)
    public Object queryTyPositiveByPage(@RequestBody HktGoodItem hktGoodItem) {
        try {
            if(hktGoodItem.getMaterialSn().length() < 3){
                return BaseResult.fail(HttpCode.BAD_REQUEST.getCode(),"物料编码长度不能小于三位");
            }
            JSONObject positiveProduct = hktPositiveEnergyService.getPositiveTyProduct(hktGoodItem);

            return BaseResult.successWithData(positiveProduct);
        } catch (Exception e) {
            logger.info("分页查询价格规则列表错误  error = {}",e.toString());
            e.printStackTrace();
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }
    private JSONObject getPositiveProduct(JSONObject positiveProduct,String value){
        HktPositivePrice positive = hktPositiveEnergyService.getPositive(new HktPositivePrice());
        if(positiveProduct == null){
            return null;
        }
        DecimalFormat dfs = new DecimalFormat("0.000000");
        JSONArray jsonObject = positiveProduct.getJSONArray("ResultList");
        JSONArray jsonArray = new JSONArray();
        for(Object obj:jsonObject){
            JSONObject jsonObject1 = JSON.parseObject(JSON.toJSONString(obj));
            if((jsonObject1.get("PartNo")+"").contains(value)){
                jsonObject1.put("PartNo",(jsonObject1.get("PartNo")+"").replaceAll(value,"<span style='color:#0067ed'>"+value+"</span>"));
            }
            JSONArray priceArray = jsonObject1.getJSONArray("StepPrices");
            List<Map> maps = JSON.parseArray(priceArray.toJSONString(), Map.class);
            List<Map> mapList = new ArrayList<>();
            for(Map map : maps){
                String price = map.get("Price")+"";
                BigDecimal bdc = new BigDecimal(price);
                BigDecimal multiply = bdc.multiply(new BigDecimal(positive.getPrice()));
                map.put("Price",dfs.format(multiply));
                mapList.add(map);
            }
            jsonObject1.put("StepPrices",mapList);
            jsonArray.add(jsonObject1);
        }
        positiveProduct.put("ResultList",jsonArray);
        return positiveProduct;
    }
}
