package com.ho.business.controller;

import com.ho.business.common.BusiTool;
import com.ho.business.entity.IncomeTemplateType;
import com.ho.business.entity.RateTemplate;
import com.ho.business.service.IncomeMappingService;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.exception.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(ContextConstant.BUSINESS + "incomeMapping")
@Api(tags = "费率配置")
public class IncomeMappingController {

    @Autowired
    BusiTool busiTool;

    @Autowired
    private IncomeMappingService incomeMappingService;

    @PostMapping("selectRateTemplates")
    @ApiOperation(value = "查询费率模板")
    public DataResult<List<RateTemplate>> selectRateTemplates(@RequestBody RateTemplate rateTemplate)
    {
        return DataResult.success(incomeMappingService.selectRateTemplates(rateTemplate));
    }

    @PostMapping("insertRateTemplate")
    @ApiOperation(value = "新增费率模板")
    @LogAnnotation(title = "费率配置", action = "新增费率模板")
    public DataResult insertRateTemplate(@RequestBody RateTemplate rateTemplate,HttpServletRequest request)
    {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser simpleUser = busiTool.getSimpleUser(token);
        return DataResult.success(incomeMappingService.insertRateTemplate(rateTemplate,simpleUser));
    }

    @PostMapping("modifyRateTemplate")
    @ApiOperation(value = "修改费率模板")
    @LogAnnotation(title = "费率配置", action = "修改费率模板")
    public DataResult modifyRateTemplate(@RequestBody RateTemplate rateTemplate,HttpServletRequest request)
    {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser simpleUser = busiTool.getSimpleUser(token);
        return DataResult.success(incomeMappingService.modifyRateTemplate(rateTemplate,simpleUser));
    }

    @PostMapping("deleteRateTemplate")
    @ApiOperation(value = "删除费率模板")
    @LogAnnotation(title = "费率配置", action = "删除费率模板")
    public DataResult deleteRateTemplate(@RequestBody RateTemplate rateTemplate)
    {
        return DataResult.success(incomeMappingService.deleteRateTemplate(rateTemplate.getId()));
    }

    @PostMapping("getIncomeTemplateTypes")
    @ApiOperation(value = "获取所有模板类型")
    public DataResult<List<IncomeTemplateType>> getIncomeTemplateTypes()
    {
        return DataResult.success(incomeMappingService.getIncomeTemplateTypes());
    }

    @PostMapping("changeSwitch")
    @ApiOperation(value = "修改模板启用状态")
    @LogAnnotation(title = "费率配置", action = "修改模板启用状态")
    public DataResult<List<IncomeTemplateType>> changeSwitch(@RequestBody RateTemplate rateTemplate)
    {
        return DataResult.success(incomeMappingService.changeSwitch(rateTemplate));
    }


}
