package io.renren.modules.admin.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Date;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.admin.entity.MoneyAccountEntity;
import io.renren.modules.admin.service.MoneyAccountService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-14 14:51:30
 */
@RestController
@RequestMapping("admin/moneyaccount")
public class MoneyAccountController {
    @Autowired
    private MoneyAccountService moneyAccountService;

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

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


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("admin:moneyaccount:info")
    public R info(@PathVariable("id") Long id){
		MoneyAccountEntity moneyAccount = moneyAccountService.getById(id);

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

    /**
     * 根据meetingId信息
     */
    @RequestMapping("/infobymid/{mid}")
    @RequiresPermissions("admin:moneyaccount:info")
    public R infoByMid(@PathVariable("mid") Long mid){
		MoneyAccountEntity moneyAccount = moneyAccountService.getOneByMeetingId(mid);
        
        return R.ok().put("moneyAccount", moneyAccount);
    }

    /**
     * 添加or修改 moneyAccount
     * @param moneyAccount
     * @return
     */
    @PostMapping("addorupdate")
    @RequiresPermissions("admin:moneyaccount:save")
    public R addOrUpdate(@RequestBody MoneyAccountEntity moneyAccount){
        Long meetingId = moneyAccount.getMeetingId();
        String name = moneyAccount.getName();
        String bank = moneyAccount.getBank();
        String accout = moneyAccount.getAccount();
        Date createTime = new Date();
        System.out.println(meetingId);
         Integer rows = moneyAccountService.addOrUpdate(meetingId, name, bank, accout, createTime);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("admin:moneyaccount:save")
    public R save(@RequestBody MoneyAccountEntity moneyAccount){
        moneyAccount.setCreateTime(new Date());
		moneyAccountService.save(moneyAccount);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:moneyaccount:update")
    public R update(@RequestBody MoneyAccountEntity moneyAccount){
		moneyAccountService.updateById(moneyAccount);

        return R.ok();
    }

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

        return R.ok();
    }

}
