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.utils.ParamUtils;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.expenseAccount.bo.InputExpenseAccount;
import com.goafanti.expenseAccount.bo.InputExpenseAccountDetails;
import com.goafanti.expenseAccount.bo.InputPageListBo;
import com.goafanti.expenseAccount.bo.OutExpenseAccountStatistics;
import com.goafanti.expenseAccount.service.ExpenseAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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;

import java.util.List;

@RestController
@RequestMapping("/api/admin/expenseAccount")
public class ExpenseAccountController extends CertifyApiController {

    @Autowired
    private ExpenseAccountService expenseAccountService;

    @Value(value = "${upload.path}")
    private final String	uploadPath			= null;

    /**
     * 新增私有报销帐号
     * @param in
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@Validated InputExpenseAccount in , BindingResult bindingResult){
        Result res =new Result();
        if (bindingResult.hasErrors()) {
            String param=bindingResult.getFieldError().getField();
            res.getError().add(buildError(ParamUtils.getParamMsg(in,param),ParamUtils.getParamName(in,param)));
            return res;
        }
        in.setAid(TokenManager.getAdminId());

        res.setData(expenseAccountService.add(in));
        return res;
    }


    /**
     * 新增私有报销帐号
     * @param in  updateType=1时，作为修改类型。会删除所有子项
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update( InputExpenseAccount in ){
        Result res =new Result();
        in.setAid(TokenManager.getAdminId());
        //只修改状态
        if (in.getUpdateType()==1){
            res.setData(expenseAccountService.updateType(in));
        }else {
            //抵扣单借支单必须指定
            if (in.getType()==5){
                if (in.getDebitId()==null){
                    res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"抵扣单借支单"));
                    return res;
                }
                //不是第三方付款和抵扣的时候必须选择报销帐号
            }else if (in.getType()!=3&&in.getType()!=5){
                if (in.getEaaid()==null){
                    res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"报销帐号"));
                    return res;
                }
            }
            res.setData(expenseAccountService.update(in));
        }
        return res;
    }

    /**
     * 报销列表
     * @param in
     * @return
     */
    @RequestMapping(value = "/pageList",method = RequestMethod.GET)
    public Result pageList(InputPageListBo in ){
        Result res =new Result();
        res.setData(expenseAccountService.pageList(in));
        return res;
    }

    /**
     * 判断当前是否存在草稿
     * @param in
     * @return
     */
    @RequestMapping(value = "/selectByPrid",method = RequestMethod.GET)
    public Result selectByPrid(InputExpenseAccount in ){
        Result res =new Result();
        res.setData(expenseAccountService.selectByPrid(in));
        return res;
    }

    /**
     * 报销列表
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectById",method = RequestMethod.GET)
    public Result selectById(Integer id ){
        Result res =new Result();
        res.setData(expenseAccountService.selectById(id));
        return res;
    }




    /**
     * 新增报销详情
     * @param in
     * @return
     */
    @RequestMapping(value = "/details/add",method = RequestMethod.POST)
    public Result addDetails(@Validated InputExpenseAccountDetails in , BindingResult bindingResult){
        Result res =new Result();
        if (bindingResult.hasErrors()) {
            String param=bindingResult.getFieldError().getField();
            System.out.println(param);
            res.getError().add(buildError(ParamUtils.getParamMsg(in,param),ParamUtils.getParamName(in,param)));
            return res;
        }
        res.setData(expenseAccountService.addDetails(in));
        return res;
    }

    /**
     * 删除报销详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/details/delete",method = RequestMethod.GET)
    public Result deleteDetails(Integer id){
        Result res =new Result();
        res.setData(expenseAccountService.deleteDetails(id));
        return res;
    }

    /**
     * 删除报销详情
     * @param eaid
     * @return
     */
    @RequestMapping(value = "/details/list",method = RequestMethod.GET)
    public Result detailsList(Integer eaid){
        Result res =new Result();
        res.setData(expenseAccountService.detailsList(eaid));
        return res;
    }

    @RequestMapping(value = "/log/list",method = RequestMethod.GET)
    public Result logList(Integer eaid){
        Result res =new Result();
        if (eaid==null){
            res.getError().add(buildErrorMessageParams(ErrorConstants.PARAM_EMPTY_ERROR,"报销编号"));
            return res;
        }
        res.setData(expenseAccountService.logList(eaid));
        return res;
    }


    /**
     * 报销审核
     * @param in
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/examine",method = RequestMethod.POST)
    public Result examine(InputExpenseAccount in,BindingResult bindingResult){
        Result res =new Result();
        if (bindingResult.hasErrors()) {
            String param=bindingResult.getFieldError().getField();
            res.getError().add(buildError(ParamUtils.getParamMsg(in,param),ParamUtils.getParamName(in,param)));
            return res;
        }
        res.setData(expenseAccountService.updateExamine(in));
        return res;
    }

    /**
     * 获取借支列表
     * @return
     */
    @RequestMapping(value = "/selectDebitOrder",method = RequestMethod.GET)
    public Result selectDebitOrder(String depId,Integer id){
        Result res=new Result();
        if (depId==null){
            res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"抵扣部门"));
            return  res;
        }
        res.setData(expenseAccountService.selectDebitOrder(depId,id));
        return res;
    }

    /**
     * 获取后台统计数据
     * @return
     */
    @RequestMapping(value = "/statistics",method = RequestMethod.GET)
    public Result statistics(String name,String depId,String startTime,String endTime){
        Result res=new Result();
        res.setData(expenseAccountService.statistics( name, depId, startTime, endTime));
        return res;
    }

    @RequestMapping(value = "/statistics/evict",method = RequestMethod.GET)
    public Result statisticsEvict(String name,String depId,String startTime,String endTime){
        Result res=new Result();
        expenseAccountService.statisticsEvict(  name, depId, startTime, endTime);
        return res.data(1);
    }


    /**
     * 获取后台统计数据
     * @return
     */
    @RequestMapping(value = "/statistics/export",method = RequestMethod.GET)
    public Result statisticsExport(String name,String depId,String startTime,String endTime){
        List<OutExpenseAccountStatistics> list = expenseAccountService.statistics( name,  depId,  startTime,  endTime);
        NewExcelUtil<OutExpenseAccountStatistics> newExcelUtil=new NewExcelUtil<>(OutExpenseAccountStatistics.class);
        return newExcelUtil.exportExcel(list,"报销统计",uploadPath);
    }



}
