package pwc.taxtech.atms.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.nutz.lang.Lang;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.customer.CustomerDto;
import pwc.taxtech.atms.dto.customer.CustomerValidateInfoDto;
import pwc.taxtech.atms.service.CustomerService;

/** @see PwC.Tax.Tech.Atms.WebApi.Controllers.CustomerController.cs */
@RestController
@RequestMapping("/api/v1/customer")
public class CustomerController {

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

    @Autowired
    private CustomerService customerService;

    @ApiOperation(value = "财务数据客户增加")
    // @ApiImplicitParam(name = "customerDtoList", value = "customerDto List",
    // required = true, dataType = "List<CustomerDto>")
    @RequestMapping(value = "/AddRange", method = RequestMethod.POST)
    public @ResponseBody List<OperationResultDto<CustomerDto>> addRange(
            @RequestBody List<CustomerDto> customerDtoList) {
        logger.debug("CustomerController /AddRange");
        return customerService.addRange(customerDtoList);
    }

    @ApiOperation(value = "财务数据账套下客户查询")
    @ApiImplicitParam(name = "ID", value = "记录ID", required = true, dataType = "java.lang.String")
    @RequestMapping(value = "/getByID", method = RequestMethod.GET)
    public @ResponseBody CustomerValidateInfoDto getByID(@RequestParam(name = "ID") String setID) {
        logger.debug("enter getByID");
        return customerService.getByEnterpriseAccountSetID(setID);
    }

    @ApiOperation(value = "财务数据所有客户查询")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public @ResponseBody List<CustomerDto> getCustomer() {
        logger.debug("enter getCustomer");
        return customerService.getCustomer();
    }

    @ApiOperation(value = "财务数据客户删除")
    // @ApiImplicitParam(name = "customerDtoList", value = "customerDto List",
    // required = true, dataType = "List<CustomerDto>")
    @RequestMapping(value = "/DeleteRange", method = RequestMethod.POST)
    public @ResponseBody OperationResultDto<String> deleteRange(@RequestBody List<CustomerDto> customerDtoList) {
        logger.debug("enter deleteRange");
        return customerService.deleteRange(customerDtoList);
    }

    @ApiOperation(value = "财务数据客户修改")
    // @ApiImplicitParam(name = "customerDtoList", value = "customerDto List",
    // required = true, dataType = "List<CustomerDto>")
    @RequestMapping(value = "/UpdateRange", method = RequestMethod.POST)
    public @ResponseBody List<OperationResultDto<CustomerDto>> updateRange(
            @RequestBody List<CustomerDto> customerDtoList) {
        logger.debug("enter updateRange");
        return customerService.updateRange(customerDtoList);
    }

    @ApiOperation(value = "批量上传账套下客户列表")
    // @ApiImplicitParam(name = "file", value = "file", required = true, dataType =
    // "CommonsMultipartFile")
    @RequestMapping(value = "/Upload", method = RequestMethod.POST)
    public @ResponseBody Object upload(@RequestParam(value = "file", required = false) CommonsMultipartFile inputFile,
            @RequestParam(value = "enterpriseAccountID", required = false) String enterpriseAccountID,
            @RequestParam(value = "action", required = false) String action) {
        logger.debug("enter upload");
        logger.debug("enterpriseAccountID: {}", enterpriseAccountID);
        logger.debug("action: {}", action);

        if (inputFile == null || inputFile.getSize() <= 0) {
            OperationResultDto<?> operationResultDto = new OperationResultDto<>();
            operationResultDto.setResult(false);
            operationResultDto.setResultMsg("NoFile");
            logger.warn("File is empty");
            return operationResultDto;
        }
        logger.debug("file name: " + inputFile.getOriginalFilename());
        InputStream input = null;
        try {
            input = inputFile.getInputStream();
        } catch (IOException e) {
            throw Lang.wrapThrow(e);
        }
        return customerService.upload(input, inputFile.getOriginalFilename(), action, enterpriseAccountID);
    }
}