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.ExpenseManagementQueryDTO;
import com.prolog.rdc.tms.model.dto.ExpenseManagmentTemplateDTO;
import com.prolog.rdc.tms.model.entity.BmsExpenseManagementModel;
import com.prolog.rdc.tms.model.vo.ExpenseManagementImportVO;
import com.prolog.rdc.tms.model.vo.ExpenseManagementQueryVO;
import com.prolog.rdc.tms.model.vo.ExpenseManagementVO;
import com.prolog.rdc.tms.model.vo.StatusUpdate;
import com.prolog.rdc.tms.service.ExpenseManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
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("/expenseManagement")
public class ExpenseManagementController {

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

    @Autowired
    private ExpenseManagementService expenseManagementService;

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

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

    @UserLog("费用管理不分页-承运商编码")
    @ApiOperation(value = "费用管理不分页", notes = "费用管理不分页")
    @PostMapping("/findListNoPage/v1.0")
    public RestMessage<List<ExpenseManagementQueryDTO>> findListNoPage(@RequestParam(value = "code") String code,@RequestParam(value = "dataType") Integer dataType) {
        List<ExpenseManagementQueryDTO> list = expenseManagementService.findListNoPage(code,dataType);
        logger.info("- > findList result={}", JsonMapper.toJsonString(list));
        return RestMessage.newInstance(true, "查询成功", list);
    }

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

    @UserLog("费用管理修改")
    @ApiOperation(value = "费用管理修改", notes = "费用管理修改")
    @PostMapping("/update/v1.0")
    public RestMessage<Object> update(@RequestBody ExpenseManagementVO vo) {
        logger.info("- > update vo={}", JsonMapper.toJsonString(vo));
        expenseManagementService.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));
        expenseManagementService.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));
        expenseManagementService.updateStatus(list);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("导出")
    @ApiOperation(value = "导出", notes = "导出")
    @PostMapping("/exportExpense/v1.0")
    public RestMessage<Object> exportExpense(HttpServletResponse response, @RequestBody ExpenseManagementQueryVO dto) throws IOException {
        logger.info("- > updateStatus vo={}", JsonMapper.toJsonString(dto));
        List<ExpenseManagmentTemplateDTO> list = expenseManagementService.exportExpense(response,dto);
        ExcelUtils.export(response, ExpenseManagmentTemplateDTO.class, "B2C销售退回单", list);
        return null;
    }

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

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