package com.pqs9900.controller.terminal;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import com.pqs9900.pojo.terminal.FlowMealDTO;
import com.pqs9900.service.terminal.FlowMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description
 * @Author cdf
 * @Date 2020/8/7 11:12
 **/
@RestController
@RequestMapping("flow")
public class FlowMealController {
    @Resource
    private UserLogDetailService userLog;

    @Autowired
    private FlowMealService flowMealService;

    /**
    * @Author chendaofei
    * @Description 新增流量套餐
    * @Date 11:33 2020/8/7
    **/
    @PostMapping("addFlowMeal")
    public HttpResult addFlowMeal(HttpServletRequest request, @RequestBody FlowMealDTO flowMealDTO){
        if(PubUtils.isBlank(flowMealDTO.getFmName())||
                flowMealDTO.getFmNumber()==null ||
                flowMealDTO.getType()==null ||flowMealDTO.getSecondType()==null){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }
       String res = flowMealService.addFlowMeal(flowMealDTO);
        if("新增成功".equals(res)){
            userLog.getLog("新增流量套餐", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
           return PubUtils.assignmentResult(null,200,res);
        }else {
            userLog.getLog("新增流量套餐", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
           return PubUtils.assignmentResult(null,500,res);
        }
    }


    /**
     * @Author chendaofei
     * @Description 修改流量套餐
     * @Date 10:41 2020/8/7
     **/
    @PostMapping("updateFlowMeal")
    public HttpResult updateFlowMeal(HttpServletRequest request,@RequestBody FlowMealDTO flowMealDTO) {
        if(PubUtils.isBlank(flowMealDTO.getFmIndex())||
                PubUtils.isBlank(flowMealDTO.getFmName())||
                flowMealDTO.getFmNumber()==null ||
                flowMealDTO.getType()==null ||flowMealDTO.getSecondType()==null){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }
        String res = flowMealService.updateFlowMeal(flowMealDTO);
        if("修改成功".equals(res)){
            userLog.getLog("修改流量套餐", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null,200,"修改成功");
        }else {
            userLog.getLog("修改流量套餐", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null,500,res);
        }
    }

    /**
     * @Author chendaofei
     * @Description 删除流量套餐
     * @Date 10:41 2020/8/7
     **/
    @PostMapping("delFlowMeal")
    public HttpResult delFlowMeal(HttpServletRequest request,String fmIndex) {
        if(PubUtils.isBlank(fmIndex)){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }
        Integer res = flowMealService.delFlowMeal(fmIndex);
        if(res == 1){
            userLog.getLog("删除流量套餐", "成功", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null,200,"删除成功");
        }else {
            userLog.getLog("删除流量套餐", "失败", PubUtils.getIpAddr(request), LogTypeEnum.SYSTEM.toString(), 1);
            return PubUtils.assignmentResult(null,500,"删除失败");
        }
    }

    /**
     * @Author chendaofei
     * @Description 获取流量套餐列表
     * @Date 10:41 2020/8/7
     **/
    @GetMapping("getFlowMealList")
    public HttpResult getFlowMealList() {
        List<FlowMealDTO> list = flowMealService.getFlowMealList();
        return PubUtils.assignmentResult(list,200,"获取成功");
    }


    /**
    * @Author chendaofei
    * @Description 通过id获取流量套餐
    * @Date 14:31 2020/8/7
    **/
    @GetMapping("getFlowMeal")
    public HttpResult getFlowMeal(String fmIndex){
        if(PubUtils.isBlank(fmIndex)){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }
        FlowMealDTO flowMealDTO = flowMealService.getFlowMeal(fmIndex);
        return PubUtils.assignmentResult(flowMealDTO,200,"获取成功");

    }

}
