package com.goafanti.expenseAccount.controller;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.model.ExpenseAccountPrivate;
import com.goafanti.common.utils.ParamUtils;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.expenseAccount.bo.InputExpenseAccountPrivate;
import com.goafanti.expenseAccount.service.ExpenseAccountPrivateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/admin/expenseAccountPrivate")
public class ExpenseAccountPrivateController extends CertifyApiController {

    @Autowired
    private ExpenseAccountPrivateService expenseAccountPrivateService;

    /**
     * 新增私有报销帐号
     * @param in
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@Validated InputExpenseAccountPrivate in , BindingResult bindingResult){
        Result res =new Result();
        if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
                    ParamUtils.getParamName(in,bindingResult.getFieldError().getField())));
            return res;
        }
        in.setAid(TokenManager.getAdminId());
        if (expenseAccountPrivateService.addCheckAccounts(in)){
            res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_BEING_ERROR,"帐号"));
            return res;
        }
        res.setData(expenseAccountPrivateService.add(in));
        return res;
    }

    /**
     * 新增私有报销帐号
     * @param in
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@Validated InputExpenseAccountPrivate in , BindingResult bindingResult){
        Result res =new Result();
        if (in.getId()==null){
            res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"我的报销帐号编号"));
            return res;
        }
        if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
                    ParamUtils.getParamName(in,bindingResult.getFieldError().getField())));
            return res;
        }
        in.setAid(TokenManager.getAdminId());
        if (expenseAccountPrivateService.updateCheckAccounts(in)){
            res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_BEING_ERROR,"帐号"));
            return res;
        }
        res.setData(expenseAccountPrivateService.update(in));
        return res;
    }
    /**
     * 私有报销帐号列表
     * @return
     */
    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST)
    public Result updateStatus(ExpenseAccountPrivate in){
        Result res =new Result();
        res.setData(expenseAccountPrivateService.updateStatus(in));
        return res;
    }
    /**
     * 私有报销帐号列表
     * @return
     */
    @RequestMapping(value = "/selectList",method = RequestMethod.GET)
    public Result selectList(Integer status,Integer pageSize,Integer pageNo){
        Result res =new Result();
        res.setData(expenseAccountPrivateService.selectList(status,pageSize,pageNo));
        return res;
    }


    @RequestMapping(value = "/details",method = RequestMethod.GET)
    public Result details(Integer id){
        Result res =new Result();
        res.setData(expenseAccountPrivateService.details(id));
        return res;
    }




}
