package com.dxhy.domestic.controller;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.R;
import com.dxhy.domestic.model.dto.AuthenticationCheckConditionDTO;
import com.dxhy.domestic.model.dto.AuthenticationConditionDTO;
import com.dxhy.domestic.model.dto.AuthenticationDTO;
import com.dxhy.domestic.model.dto.AuthenticationImportDTO;
import com.dxhy.domestic.model.vo.*;
import com.dxhy.domestic.service.drawback.DomesticInvoiceDrawbackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 增值税发票退税认证接口 手工认证、导入认证、认证查询
 *
 * @author dxhy
 */
@RestController
@RequestMapping("/tax_drawback")
@Slf4j
@Api(tags = "退税手工验证页面接口")
public class DomesticInvoiceDrawbackController extends AbstractController {

    @Resource
    private DomesticInvoiceDrawbackService domesticInvoiceDrawbackService;

    /**
     * 退税手工认证列表接口
     *
     * @param dto
     *            参数
     */
    @PostMapping("/authentication_list")
    @ApiOperation(value = "退税手工认证列表接口")
    public ResponseEntity<R> authenticationList(@RequestBody @Valid AuthenticationConditionDTO dto) {
        CommonListResponseVO<ManualAuthenticationListVO> resultVO =
            domesticInvoiceDrawbackService.authenticationList(dto, getUserInfo());
        return ResponseEntity.ok(R.ok().put("data", responseResult(JSON.toJSONString(resultVO))));
    }

    /**
     * 退税手工认证接口
     *
     * @param dto
     *            参数
     */
    @PostMapping("/authentication")
    @ApiOperation(value = "退税手工认证接口")
    public ResponseEntity<R> authentication(@Valid @RequestBody AuthenticationDTO dto) {

        AuthenticationResultVO vo = domesticInvoiceDrawbackService.authentication(dto, getUserInfo(), getUserName());
        return ResponseEntity.ok(R.ok().put("data", responseResult(JSON.toJSONString(vo))));
    }

    /**
     * 退税导入认证信息查询接口
     *
     * @param datalist
     *            参数
     */
    @ResponseBody
    @PostMapping("/import_authentication_check")
    @ApiOperation(value = "退税导入认证信息查询接口")
    public ResponseEntity<R> importAuthenticationCheck(@RequestBody Map<String, String> datalist) {

        List<AuthenticationImportDTO> dtoList =
            JSON.parseArray(datalist.get("datalist"), AuthenticationImportDTO.class);
        dtoList.forEach(dto -> {
            dto.setInvoiceCode(StringUtils.isBlank(dto.getInvoiceCode())?"":dto.getInvoiceCode().replace(" ", ""));
            dto.setInvoiceDate(dto.getInvoiceDate().replace(" ", ""));
            dto.setInvoiceNo(dto.getInvoiceNo().replace(" ", ""));
            dto.setTaxAmount(dto.getTaxAmount().replace(" ", ""));
        });
        String menuId = datalist.get("menuId");
        CommonListResponseVO<ImportAuthenticationListVO> resultList =
            domesticInvoiceDrawbackService.importAuthenticationCheck(dtoList, getUserInfo(), menuId);
        return ResponseEntity.ok(R.ok().put("data", responseResult(JSON.toJSONString(resultList))));
    }

    /**
     * 退税认证查询接口
     *
     * @param dto
     *            参数
     */
    @PostMapping("/authentication_check")
    @ApiOperation(value = "退税认证查询接口")
    public ResponseEntity<R> authenticationCheck(@Valid @RequestBody AuthenticationCheckConditionDTO dto) {
        if (StringUtils.isEmpty(dto.getCompany())) {
            dto.setCompany(getUserInfo().getCompany());
        }
        CommonListResponseVO<AuthenticationCheckListVO> responseVO =
            domesticInvoiceDrawbackService.authenticationCheck(dto, getUserInfo());
        return ResponseEntity.ok(R.ok().put("data", responseResult(JSON.toJSONString(responseVO))));
    }

    /**
     * 请求结果加密封装
     *
     * @param json
     *            json
     */
    private String responseResult(String json) {
        String jsonString = null;
        if (json != null) {
            jsonString = Base64.encode(json);
        }

        return jsonString;
    }
}
