package io.renren.modules.admin.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

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

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.admin.entity.TypesOfFeeEntity;
import io.renren.modules.admin.service.TypesOfFeeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 费用类型表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-24 10:38:03
 */
@RestController
@RequestMapping("admin/typesoffee")
public class TypesOfFeeController {
    @Autowired
    private TypesOfFeeService typesOfFeeService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:typesoffee:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = typesOfFeeService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/listbymid/{mid}")
    @RequiresPermissions("admin:typesoffee:list")
    public R listByMeetingId(@PathVariable("mid") Long mid){
        List<TypesOfFeeEntity> list = typesOfFeeService.list(new QueryWrapper<TypesOfFeeEntity>().eq("meeting_id", mid));

        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("admin:typesoffee:info")
    public R info(@PathVariable("id") Long id){
		TypesOfFeeEntity typesOfFee = typesOfFeeService.getById(id);

        return R.ok().put("typesOfFee", typesOfFee);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("admin:typesoffee:save")
    public R save(@RequestBody TypesOfFeeEntity typesOfFee){
        BigDecimal maxMoney = BigDecimal.valueOf(999999.99);
        if (typesOfFee.getMoney().compareTo(maxMoney) == 1) {
            return R.error("金额超出可使用范围");
        }

        typesOfFee.setCreateTime(new Date());
		typesOfFeeService.save(typesOfFee);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:typesoffee:update")
    public R update(@RequestBody TypesOfFeeEntity typesOfFee){
		typesOfFeeService.updateById(typesOfFee);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:typesoffee:delete")
    public R delete(@RequestBody Long[] ids){
		typesOfFeeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
