package com.goafanti.expenseAccount.controller;

import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.expenseAccount.bo.ExpenseCountsBo;
import com.goafanti.expenseAccount.bo.ExpenseCountsDep;
import com.goafanti.expenseAccount.bo.ExpenseCountsInput;
import com.goafanti.expenseAccount.bo.ExpenseCountsOrder;
import com.goafanti.expenseAccount.service.ExpenseCountsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/admin/expenseCounts")
public class ExpenseCountsController extends CertifyApiController {

    @Autowired
    private ExpenseCountsService expenseAccountService;
    @Value(value = "${upload.path}")
    private final String	uploadPath			= null;
    @RequestMapping(value = "/getCounts",method = RequestMethod.GET)
    public Result<ExpenseCountsBo> getCounts(ExpenseCountsInput in){
        Result res=new Result();
        if (in.getEndTime() != null)in.setEndTime(in.getEndTime()+ " 23:59:59");
        return res.data(expenseAccountService.getCounts(in));
    }


    @RequestMapping(value = "/getCounts/Export",method = RequestMethod.GET)
    public Result<ExpenseCountsBo> getCountsExport(ExpenseCountsInput in){
        NewExcelUtil excelUtil =null;
        if (in.getEndTime() != null)in.setEndTime(in.getEndTime()+ " 23:59:59");
        List<ExpenseCountsBo> list  = (List<ExpenseCountsBo>) expenseAccountService.getCounts(in);
        if (in.getTargetType()==1){
            excelUtil=new NewExcelUtil(ExpenseCountsOrder.class);
            List<ExpenseCountsOrder>orderList = new ArrayList<>();
            for (ExpenseCountsBo expenseCountsBo : list) {
                ExpenseCountsOrder order = new ExpenseCountsOrder();
                BeanUtils.copyProperties(expenseCountsBo,order);
                orderList.add(order);
            }
            return excelUtil.exportExcel(orderList, "报销总计", uploadPath);
        }else {
            excelUtil=new NewExcelUtil(ExpenseCountsDep.class);
            List<ExpenseCountsDep>depList = new ArrayList<>();
            for (ExpenseCountsBo expenseCountsBo : list) {
                ExpenseCountsDep dep=new ExpenseCountsDep();
                BeanUtils.copyProperties(expenseCountsBo,dep);
                depList.add(dep);
            }
            return excelUtil.exportExcel(depList, "报销总计", uploadPath);
        }
    }

    @RequestMapping(value = "/getCountsClear",method = RequestMethod.GET)
    public Result<ExpenseCountsBo> getCountsClear(){
        Result res=new Result();
        return res.data(expenseAccountService.getCountsClear());
    }
}
