package com.pqs9900.serviceImpl.terminal;


import cn.hutool.core.collection.CollectionUtil;
import com.njcn.shiro.token.TokenManager;
import com.pqs9900.mapper.terminal.FlowMealMapper;
import com.pqs9900.pojo.terminal.FlowMealDTO;
import com.pqs9900.service.terminal.FlowMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description
 * @Author cdf
 * @Date 2020/8/7 10:40
 **/
@Service
public class FlowMealServiceImpl implements FlowMealService {

    @Autowired
    private FlowMealMapper flowMealMapper;

    /**
    * @Author chendaofei
    * @Description 新增流量套餐
    * @Date 10:41 2020/8/7
    **/
    @Override
    public String addFlowMeal(FlowMealDTO flowMealDTO){
        Integer type = flowMealDTO.getType();
        Integer secondType = flowMealDTO.getSecondType();
        if(type == 0 && secondType==0){
            //基础套餐，判断是否存在默认套餐
            List<FlowMealDTO> res = flowMealMapper.getFlowMealList(null,null,0,0);
            if(!CollectionUtil.isEmpty(res)){
               return "已存在基础默认套餐";
            }
        }
        List<FlowMealDTO> list = flowMealMapper.getFlowMealList(null,flowMealDTO.getFmName(),null,null);
        if(list!=null && list.size()>0){
            return "已存在此套餐名";
        }
        String userid= TokenManager.getUserId();
        String uuid = UUID.randomUUID().toString();
        flowMealDTO.setFmIndex(uuid);
        flowMealDTO.setUpdateUser(userid);
        flowMealDTO.setUpdateTime(new Date());
       Integer res = flowMealMapper.insert(flowMealDTO);
        if(res == 1){
            return "新增成功";
        }else {
            return "新增失败";
        }
    }

    /**
     * @Author chendaofei
     * @Description 修改流量套餐
     * @Date 10:41 2020/8/7
     **/
    @Override
    public String updateFlowMeal(FlowMealDTO flowMealDTO) {
        Integer type = flowMealDTO.getType();
        Integer secondType = flowMealDTO.getSecondType();
        if(type == 0 && secondType==0){
            //基础套餐，判断是否存在默认套餐
            List<FlowMealDTO> res = flowMealMapper.getFlowMealList(flowMealDTO.getFmIndex(),null,0,0);
            if(!CollectionUtil.isEmpty(res)){
                return "已存在基础默认套餐";
            }
        }
        List<FlowMealDTO> list = flowMealMapper.getFlowMealList(flowMealDTO.getFmIndex(),flowMealDTO.getFmName(),null,null);
        if(!CollectionUtil.isEmpty(list)){
            return "已存在此套餐名";
        }
        String userid= TokenManager.getUserId();
        String uuid = UUID.randomUUID().toString();
        flowMealDTO.setUpdateUser(userid);
        flowMealDTO.setUpdateTime(new Date());
        Integer res = flowMealMapper.updateByPrimaryKeySelective(flowMealDTO);
        if(res == 1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }

    /**
     * @Author chendaofei
     * @Description 删除流量套餐
     * @Date 10:41 2020/8/7
     **/
    @Override
    public Integer delFlowMeal(String fmIndex) {
       return flowMealMapper.deleteByPrimaryKey(fmIndex);
    }

    /**
     * @Author chendaofei
     * @Description 获取流量套餐
     * @Date 10:41 2020/8/7
     **/
    @Override
    public FlowMealDTO getFlowMeal(String fmIndex) {
        return flowMealMapper.selectByPrimaryKey(fmIndex);
    }

    /**
     * @Author chendaofei
     * @Description 获取流量套餐列表
     * @Date 10:41 2020/8/7
     **/
    @Override
    public List<FlowMealDTO> getFlowMealList() {
       return flowMealMapper.getFlowMealList(null,null,null,null);
    }


}
