package com.prolog.rdc.tms.controller;

import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.product.core.easyexcel.util.ExcelUtils;
import com.prolog.product.core.tools.util.JsonMapper;
import com.prolog.rdc.tms.model.dto.ExpenseAccountQueryDTO;
import com.prolog.rdc.tms.model.vo.ExpenseAccountImportVO;
import com.prolog.rdc.tms.model.vo.ExpenseAccountQueryVO;
import com.prolog.rdc.tms.model.vo.ExpenseAccountVO;
import com.prolog.rdc.tms.model.vo.StatusUpdate;
import com.prolog.rdc.tms.service.ExpenseAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "费用科目")
@RequestMapping("/expenseAccount")
public class ExpenseAccountController {

    private final Logger logger = LoggerFactory.getLogger(ExpenseAccountController.class);

    @Autowired
    private ExpenseAccountService expenseAccountService;

    @UserLog("费用科目新增")
    @ApiOperation(value = "费用科目新增", notes = "费用科目新增")
    @PostMapping("/save/v1.0")
    public RestMessage<Object> save(@RequestBody @Valid ExpenseAccountVO vo) {
        logger.info("- > createExpenseAccount vo={}", JsonMapper.toJsonString(vo));
        expenseAccountService.createExpenseAccount(vo);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("费用科目分页查询")
    @ApiOperation(value = "费用科目分页查询", notes = "费用科目分页查询")
    @PostMapping("/findList/v1.0")
    public RestMessage<Page<ExpenseAccountQueryDTO>> findList(@RequestBody @Valid ExpenseAccountQueryVO vo) {
        logger.info("- > queryExpenseAccount vo={}", JsonMapper.toJsonString(vo));
        Page<ExpenseAccountQueryDTO> list = expenseAccountService.queryExpenseAccount(vo);
        logger.info("- > queryExpenseAccount result={}", JsonMapper.toJsonString(list));
        return RestMessage.newInstance(true, "查询成功", list);
    }

    @UserLog("费用科目详情")
    @ApiOperation(value = "费用科目详情", notes = "费用科目详情")
    @PostMapping("/detail/v1.0")
    public RestMessage<ExpenseAccountQueryDTO> detail(@RequestParam("id") Long id) {
        logger.info("- > queryExpenseAccount vo={}", JsonMapper.toJsonString(id));
        ExpenseAccountQueryDTO detail = expenseAccountService.detail(id);
        logger.info("- > queryExpenseAccount result={}", JsonMapper.toJsonString(detail));
        return RestMessage.newInstance(true, "查询成功", detail);
    }

    @UserLog("费用科目修改")
    @ApiOperation(value = "费用科目详情", notes = "费用科目详情")
    @PostMapping("/update/v1.0")
    public RestMessage<Object> update(@RequestBody ExpenseAccountVO vo) {
        logger.info("- > update vo={}", JsonMapper.toJsonString(vo));
        expenseAccountService.update(vo);
        return RestMessage.newInstance(true, "查询成功", null);
    }

    @UserLog("费用科目删除")
    @ApiOperation(value = "费用科目删除", notes = "费用科目删除")
    @PostMapping("/delete/v1.0")
    public RestMessage<Object> delete(@RequestBody Long[] ids) {
        logger.info("- > delete vo={}", JsonMapper.toJsonString(ids));
        expenseAccountService.delete(ids);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("费用科目启用停用")
    @ApiOperation(value = "费用科目启用停用", notes = "费用科目启用停用")
    @PostMapping("/updateStatus/v1.0")
    public RestMessage<Object> updateStatus(@RequestBody List<StatusUpdate> list) {
        logger.info("- > updateStatus vo={}", JsonMapper.toJsonString(list));
        expenseAccountService.updateStatus(list);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("导入")
    @ApiOperation(value = "导入", notes = "导入")
    @PostMapping("/importExpense/v1.0")
    public RestMessage<Object> importExpense(@RequestParam("file") MultipartFile file) throws IOException {
        expenseAccountService.importExpense(file);
        return RestMessage.newInstance(true, "导入成功", null);
    }

    @UserLog("模版下载")
    @ApiOperation(value = "模版下载", notes = "模版下载")
    @PostMapping("/downloadTemplate/v1.0")
    public RestMessage<Object> downloadTemplate(HttpServletResponse response) throws IOException {
        List<ExpenseAccountImportVO> list = new ArrayList<>();
        ExcelUtils.export(response, ExpenseAccountImportVO.class,  "费用科目", list,null,null,true);
        return null;
    }
}