package com.dxhy.core.controller;

import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.exception.BaseException;
import com.dxhy.common.utils.R;
import com.dxhy.core.service.ywgl.YwglService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @description  运维管理控制层
 * @author  Allen
 * @date    2024/5/11 14:51
 * @return
*/
@SuppressWarnings({"AlibabaUndefineMagicConstant", "DuplicatedCode"})
@RestController
@RequestMapping("/ywgl")
@Slf4j
public class YwglController extends AbstractController {

    @Autowired
    private YwglService YwglService;
    @RequestMapping(value = "/fpsltj", method = RequestMethod.POST)
    public ResponseEntity<R> fpsltj(@RequestBody Map<String,String> map) {
        log.info("查询发票数量统计表开始，入参：{}",map);
        try {
            Map<String, Object> retMap = YwglService.fpsltj(map);
            return ResponseEntity.ok(R.ok().put("data", retMap));
        } catch (Exception e) {
            log.info("查询发票数量统计表业务处理异常：{}", e);
            return ResponseEntity.ok(R.error("业务处理异常"));
        }
    }

    @RequestMapping(value = "/fpztSgtb", method = RequestMethod.POST)
    public ResponseEntity<R> fpztSgtb(@RequestBody Map<String,String> map) {
        log.info("发票状态手工同步开始，入参：{}",map);
        try {
            String fpdm = map.get("fpdm");
            String fphm = map.get("fphm");
            String sdhm = null;
            if (StringUtils.isBlank(fpdm) && StringUtils.isNotBlank(fphm) && fphm.length() == 20) {
                //判断为数电号码
                sdhm = fphm;
            }
            List<Map<String, String>> retMap = YwglService.fpztSgtb(fpdm,fphm,sdhm);
            return ResponseEntity.ok(R.ok().put("data", retMap));
        } catch (Exception e) {
            if (e instanceof BaseException) {
                return ResponseEntity.ok(R.error("业务处理失败："+e.getMessage()));
            }
            log.info("发票状态手工同步处理异常：{}", e);
            return ResponseEntity.ok(R.error("业务处理异常："+e.getMessage()));
        }
    }

    /**
     * @description  发票状态导入同步
     * @author  Allen
     * @date    2024/5/15 10:43
     * @return
    */
    @RequestMapping(value = "/fpztdrtb", method = RequestMethod.POST)
    public ResponseEntity<R> fpztdrtb(@RequestBody List<Map<String,String>> reqList) {
        log.info("发票状态导入同步开始，入参：{}",reqList);
        try {
            return YwglService.fpztdrtb(reqList);
        } catch (Exception e) {
            log.info("发票状态导入同步处理异常：{}", e);
            return ResponseEntity.ok(R.error("业务处理异常"));
        }
    }
}
