package com.yijia.customer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.enums.BusinessType;
import com.yijia.customer.domain.CustomerGradeSetting;
import com.yijia.customer.service.ICustomerGradeSettingService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 客户优惠等级设置Controller
 *
 * @author yijia
 * @date 2020-12-21
 */
@RestController
@RequestMapping("/customer/setting")
public class CustomerGradeSettingController extends BaseController
{
    @Autowired
    private ICustomerGradeSettingService customerGradeSettingService;

    /*
     * 查询成长规则列表
     */
    @RequestMapping("/getGrouthRuleList")
    public TableDataInfo getGrouthRuleList(){
        //分页
        startPage();
        List<Map<String, Object>> grouthRuleList = customerGradeSettingService.getGrouthRuleList();
        return getDataTable(grouthRuleList);
    }

    /*
     * 新增成长规则
     * @param jsonObject
     */
    @RequestMapping("/addGrouthRule")
    @Log(title = "客户等级成长规则新增", businessType = BusinessType.INSERT)
    public AjaxResult addGrouthRule(@RequestBody  JSONObject jsonObject){
        Map<String, Object> params = new HashMap();
        params.put("grouthValueName", jsonObject.get("grouthValueName"));
        params.put("grouthValueConsume", jsonObject.get("grouthValueConsume"));
        params.put("grouthValue", jsonObject.get("grouthValue"));
        return toAjax(customerGradeSettingService.insertGrouthRule(params));
    }

    /*
     * 编辑成长规则
     * @param jsonObject
     */
    @RequestMapping("/editGrouthRule")
    @Log(title = "客户等级成长编辑", businessType = BusinessType.UPDATE)
    public AjaxResult editGrouthRule(@RequestBody  JSONObject jsonObject){
        Map<String, Object> params = new HashMap();
        params.put("id", jsonObject.get("id"));
        params.put("grouthValueName", jsonObject.get("grouthValueName"));
        params.put("grouthValueConsume", jsonObject.get("grouthValueConsume"));
        params.put("grouthValue", jsonObject.get("grouthValue"));
        return toAjax(customerGradeSettingService.updateGrouthRule(params));
    }

    /*
     * 删除成长规则
     * @param id
     */
    @Log(title = "客户等级成长规则删除", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/delGrouthRule",method = RequestMethod.DELETE)
    public AjaxResult delGrouthRule(Long[] id){
        return toAjax(customerGradeSettingService.deleteGrouthRuleByIds(id));
    }



    /**
     * 查询客户优惠等级设置列表
     */
    @PreAuthorize("@ss.hasPermi('customer:setting:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerGradeSetting customerGradeSetting)
    {
        startPage();
        List<CustomerGradeSetting> list = customerGradeSettingService.selectCustomerGradeSettingList(customerGradeSetting);
        return getDataTable(list);
    }

    /**
     * 导出客户优惠等级设置列表
     */
    @PreAuthorize("@ss.hasPermi('customer:setting:export')")
    @Log(title = "客户优惠等级设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomerGradeSetting customerGradeSetting)
    {
        List<CustomerGradeSetting> list = customerGradeSettingService.selectCustomerGradeSettingList(customerGradeSetting);
        ExcelUtil<CustomerGradeSetting> util = new ExcelUtil<CustomerGradeSetting>(CustomerGradeSetting.class);
        return util.exportExcel(list, "setting");
    }

    /**
     * 获取客户优惠等级设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:setting:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerGradeSettingService.selectCustomerGradeSettingById(id));
    }

    /**
     * 新增客户优惠等级设置
     */
    @PreAuthorize("@ss.hasPermi('customer:setting:add')")
    @Log(title = "客户优惠等级设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerGradeSetting customerGradeSetting)
    {
        return toAjax(customerGradeSettingService.insertCustomerGradeSetting(customerGradeSetting));
    }

    /**
     * 修改客户优惠等级设置
     */
    @PreAuthorize("@ss.hasPermi('customer:setting:edit')")
    @Log(title = "客户优惠等级设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerGradeSetting customerGradeSetting)
    {
        return toAjax(customerGradeSettingService.updateCustomerGradeSetting(customerGradeSetting));
    }

    /**
     * 删除客户优惠等级设置
     */
    @PreAuthorize("@ss.hasPermi('customer:setting:remove')")
    @Log(title = "客户优惠等级设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerGradeSettingService.deleteCustomerGradeSettingByIds(ids));
    }
}
