package com.dxhy.customs.controller;

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

import javax.annotation.Resource;

import com.dxhy.common.util.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.vo.Business;
import com.dxhy.customs.entity.TdxCustomsRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.R;
import com.dxhy.customs.model.TdxCustomsRecordDTO;
import com.dxhy.customs.service.CustomsSignService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dxhy
 */
@RestController
@RequestMapping("/sign")
@Slf4j
public class CustomsSignController extends AbstractController {
    @Resource
    private CustomsSignService customsSignService;

    @PostMapping("/queryManualSign")
    @ResponseBody
    @SysLog("缴款书手工签收-查询列表")
    public ResponseEntity<?> queryManualSign(Page<?> page, @RequestBody TdxCustomsRecordDTO entity) {
        try {
            return ResponseEntity
                .ok(R.ok().put("data", customsSignService.queryManualSign(page, entity, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书手工签收-查询列表", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/batchSign")
    @ResponseBody
    @SysLog("缴款书手工签收-批量签收")
    public ResponseEntity<?> batchSign(@RequestBody String costomsInfo) {
        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", customsSignService.batchSign(costomsInfo, this.getUserInfo()))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书手工签收-批量签收", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/scanBatchSign")
    @ResponseBody
    @SysLog("缴款书 查询签收-批量签收")
    public ResponseEntity<?> scanBatchSign(@RequestBody List<String> idList) {
        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", customsSignService.scanBatchSign(idList, this.getUserInfo()))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书手工签收-批量签收", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/cancelSign")
    @ResponseBody
    @SysLog("缴款书签收查询-取消签收")
    public ResponseEntity<?> cancelSign(@RequestBody List<String> idList) {
        try {
            return ResponseEntity.ok(R.ok().put("data", customsSignService.cancelSign(idList, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书签收查询-取消签收", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/changeBusiness")
    @ResponseBody
    @SysLog("缴款书签收查询-修改业务单元")
    public ResponseEntity<?> changeBusiness(@RequestBody TdxCustomsRecordDTO entity) {
        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", customsSignService.changeBusiness(entity, this.getUserInfo()))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书签收查询-修改业务单元", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/changeRemark")
    @ResponseBody
    @SysLog("缴款书签收查询-修改备注")
    public ResponseEntity<?> changeRemark(@RequestBody TdxCustomsRecordDTO entity) {
        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", customsSignService.changeRemark(entity, this.getUserInfo()))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书签收查询-修改备注", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/infoCollect")
    @ResponseBody
    @SysLog("缴款书导入签收-信息采集")
    public ResponseEntity<?> infoCollect(@RequestBody String costomsInfo) {
        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", customsSignService.infoCollect(costomsInfo, this.getUserInfo()))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书导入签收-信息采集", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/importSign")
    @ResponseBody
    @SysLog("缴款书导入签收-导入")
    public ResponseEntity<?> importSign(@RequestBody Map<String, String> pramsMap) {
        try {
            return ResponseEntity.ok(R.ok().put("data", customsSignService.importSign(pramsMap, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书导入签收-导入", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @RequestMapping("/deleteCustomScan")
    @SysLog("删除签收失败发票")
    public ResponseEntity<R> deleteCustomScan(@RequestBody Map<String, String> pramsMap) {
        try {
            // 删除 扫描表信息
            boolean deleteElec = customsSignService.deleteCustomScan(pramsMap.get("scanId"), getUserInfo().getDbName());
            if (deleteElec) {
                return ResponseEntity.ok(R.ok().put("data", "删除成功！"));
            } else {
                return ResponseEntity.ok(R.error("删除失败！"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/querySign")
    @ResponseBody
    @SysLog("缴款书签收-查询列表")
    public ResponseEntity<?> querySign(Page<?> page, @RequestBody TdxCustomsRecordDTO entity) {
        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", customsSignService.querySign(page, entity, this.getUserInfo()))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书签收-查询列表", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 缴款书编辑
     *
     * @author 陈刚扬
     * @date 2021/2/3
     * @param entity
     * @return org.springframework.http.ResponseEntity<?>
     */
    @PostMapping("/editCustoms")
    @ResponseBody
    @SysLog("海关缴款书签收-录入修改")
    public ResponseEntity<?> editCustoms(@RequestBody TdxCustomsRecord entity) {
        try {
            return ResponseEntity.ok(R.ok().put("data", customsSignService.editCustoms(entity, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("海关缴款书签收录入修改", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/scanSign")
    @ResponseBody
    @SysLog("缴款书签收-扫描仪签收")
    public ResponseEntity<?> scanSign(@RequestBody Map<String, String> pramsMap) {

        if (StringHelper.isBlank(pramsMap.get("pch"))) {

            return ResponseEntity.ok(R.error("扫描识别批次号不能为空！"));
        }

        try {
            return ResponseEntity.ok(R.ok().put("data", customsSignService.scanSign(pramsMap, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书扫描签收", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }
}
