package pwc.taxtech.atms.controller;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
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 pwc.taxtech.atms.dto.stdaccount.StandardAccountDto;
import pwc.taxtech.atms.dto.stdaccount.StdAccountFancyTreeDto;
import pwc.taxtech.atms.service.impl.StdAccountServiceImpl;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api/v1/stdAccount")
public class StdAccountController extends BaseController {

    @Autowired
    private StdAccountServiceImpl stdAccountService;

    @ResponseBody
    @ApiOperation(value = "获取科目层级")
    @RequestMapping(value = "stdAccountHierarchy", method = RequestMethod.GET)
    public List<StdAccountFancyTreeDto> getStdAccountHierarchy(@RequestParam(name = "orgID") String orgId) {
        if (StringUtils.isBlank(orgId)) {
            return Collections.emptyList();
        }

        try {
            return stdAccountService.getStdAccountHierarchy(orgId);
        } catch (Exception e) {
            logger.error("getStdAccountHierarchy error.", e);
        }
        return Collections.emptyList();
    }

    @ResponseBody
    @ApiOperation(value = "查看对应关系")
    @RequestMapping(value = "getStdAccountLinkEtsAccount", method = RequestMethod.GET)
    public List<StandardAccountDto> getStdAccountLinkEtsAccount(@RequestParam String orgId,
                                                                @RequestParam String accountSetId) {
        if (StringUtils.isAnyBlank(orgId, accountSetId)) {
            return Collections.emptyList();
        }
        try {
            return stdAccountService.GetStdAccountLinkEtsAccount(orgId, accountSetId);
        } catch (Exception e) {
            logger.error("getStdAccountLinkEtsAccount error.", e);
        }
        return Collections.emptyList();
    }

    @ApiOperation(value = "根据行业查找科目")
    @RequestMapping(value = "stdAccount/byIndustry/{industryId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    List<StandardAccountDto> getStdAccountByIndustry(@PathVariable String industryId) {
        if (StringUtils.isAnyBlank(industryId)) {
            return Collections.emptyList();
        }

        try {
            return stdAccountService.getStdAccountByIndustry(industryId);
        } catch (Exception e) {
            logger.error("GetStdAccountByIndustry error.", e);
        }
        return Collections.emptyList();
    }
}