package com.bjtdba.merchant.controller;

import com.bjtdba.entity.merchant.SystemConfigValue;
import com.bjtdba.merchant.service.SystemConfigValueService;
import com.bjtdba.utils.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 配置表(SystemConfigValue)表控制层
 *
 * @author makejava
 * @since 2022-08-07 14:40:43
 */
@RestController
@RequestMapping("/merchant_service/systemConfigValue")
public class SystemConfigValueController {
    /**
     * 服务对象
     */
    @Resource
    private SystemConfigValueService systemConfigValueService;

    private  static final Logger loger= (Logger) LoggerFactory.getLogger(SystemConfigValueController.class);

    /**
     * 查询当前门店的分润比例设置
     * @param map
     * @return
     */
    @PostMapping ("/queyrConfig")
    public Object queryConfig(@RequestBody Map<String,Object> map ){
        if(!map.containsKey("merId")){
            return  ResponseUtil.fail();
        }
        int merId=Integer.parseInt(map.get("merId")+"");
        String key=map.get("key")+"";
        loger.info("queryConfig request param merId:"+merId+"key:"+key);
        Object ob=new Object();
        SystemConfigValue systemConfigValue=new SystemConfigValue();//设置条件的
        SystemConfigValue systemConfigValueObj=new SystemConfigValue();//返回
        Map<String,Object> data=new HashMap<String, Object>();
        List<SystemConfigValue> systemConfigValueList= new  ArrayList<SystemConfigValue>();

        //参数不能为空
        if(merId!=0&&!("".equals(key))) {
            systemConfigValue.setConfigKey(key);
            systemConfigValue.setMerId(merId);
            systemConfigValueList = systemConfigValueService.queryAll(systemConfigValue);
            if (systemConfigValueList != null&&systemConfigValueList.size()!=0) {
                systemConfigValueObj = systemConfigValueList.get(0);
            }else{
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                //添加进去
                SystemConfigValue rrxSystemConfigValue = new SystemConfigValue();
                rrxSystemConfigValue.setConfigKey("base_commission");
                rrxSystemConfigValue.setValue("5");
                rrxSystemConfigValue.setCreateTime(new Date());
                rrxSystemConfigValue.setMerId(merId);
                systemConfigValueService.insert(rrxSystemConfigValue);
                systemConfigValueList = systemConfigValueService.queryAll(systemConfigValue);
                systemConfigValueObj = systemConfigValueList.get(0);
            }
            data.put("systemConfig", systemConfigValueObj);
            ob=ResponseUtil.ok(data);
        }else{
            ob=ResponseUtil.fail();
        }

        loger.info("queryConfig return "+ob);
        return  ob;

    }

    /**
     * 根据配置id修改当前门户的分润比例
     * @param map
     * @return
     */
    @PostMapping("/upConfig")
    public Object upConfig(@RequestBody Map<String,Object> map){
        if(!map.containsKey("configValueId")){
            return  ResponseUtil.fail();
        }
        Object ob=new Object();
        int configValueId=Integer.parseInt(map.get("configValueId")+"");
        String value=map.get("value")+"";
        loger.info("upConfig request param configValueId:"+configValueId+"value:"+value);
        if("".equals(value)){
            return ResponseUtil.fail();
        }
        Double objvalue=Double.parseDouble(value);
        if(objvalue<5||objvalue>100){
            return ResponseUtil.fail("您已经开通消分设置，您的分润值，应该大于等5");
        }

        SystemConfigValue systemConfigValue=new SystemConfigValue();

        if(configValueId!=0&&!("".equals(value))){
            systemConfigValue=systemConfigValueService.queryById(configValueId);
            if(systemConfigValue.getValue().equals("2")){
                if(objvalue<2){
                    ob=ResponseUtil.fail("您已经开通消分设置，您的分润值，应该大于2");
                    return  ob;
                }
            }
            systemConfigValue.setValue(value);
            SystemConfigValue systemConfigValueIsno=new SystemConfigValue();
            systemConfigValueIsno=systemConfigValueService.update(systemConfigValue);
            if(systemConfigValueIsno.getValue().equals(value)){
                ob=ResponseUtil.ok();
            }else{
                ob=ResponseUtil.fail();
            }

        }else{
            ob= ResponseUtil.fail();
        }
        loger.info("upConfig return "+ob);
        return  ob;
    }

    /**
     * 获取配置
     */
    @PostMapping("/getShopBonusSetting")
    public Object getShopBonusSetting(@RequestBody Map<String,String> map){

        return ResponseUtil.ok(systemConfigValueService.getShopBonusSetting(map.get("mer_id"))) ;
    }
    /**
     * 获取配置
     */
    @PostMapping("/updateShopBonusSetting")
    public Object updateShopBonusSetting(@RequestBody Map<String,String> map){
        return ResponseUtil.ok(systemConfigValueService.updateShopBonusSetting(map.get("mer_id"),map.get("value")));
    }
}
