package com.dxhy.customs.controller;

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

import javax.annotation.Resource;

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.util.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.R;
import com.dxhy.customs.entity.TdxCustomsRecord;
import com.dxhy.customs.model.TdxCustomsRecordDTO;
import com.dxhy.customs.service.CustomsRecordService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dxhy
 */
@RestController
@RequestMapping("/custom")
@Slf4j
public class CustomsPaymentController extends AbstractController {

    @Resource
    private CustomsRecordService customsRecordService;

    /**
     * 海关缴款书采集-查询列表
     *
     * @author 陈刚扬
     * @date 2020/8/6
     * @param page
     *            入参
     * @param entity
     *            入参
     * @return org.springframework.http.ResponseEntity<?>
     */
    @PostMapping("/query")
    @ResponseBody
    @SysLog("海关缴款书采集-查询列表")
    public ResponseEntity<?> query(Page<?> page, @RequestBody TdxCustomsRecordDTO entity) {
        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", customsRecordService.query(page, entity, this.getUserInfo()))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("海关缴款书采集-查询列表", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 海关缴款书采集-详情列表
     *
     * @author 陈刚扬
     * @date 2020/8/6
     * @param page
     *            入参
     * @param entity
     *            入参
     * @return org.springframework.http.ResponseEntity<?>
     */
    @PostMapping("/detail")
    @ResponseBody
    @SysLog("海关缴款书采集-详情列表")
    public ResponseEntity<?> detail(Page<?> page, @RequestBody TdxCustomsRecordDTO entity) {
        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", customsRecordService.detail(page, entity, this.getUserInfo()))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("海关缴款书采集-详情列表", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 异常海关缴款书-详情列表
     *
     * @author 陈刚扬
     * @date 2020/8/6
     * @param page
     *            入参
     * @param entity
     *            入参
     * @return org.springframework.http.ResponseEntity<?>
     */
    @PostMapping("/exceptionDetail")
    @ResponseBody
    @SysLog("异常海关缴款书-详情列表")
    public ResponseEntity<?> exceptionDetail(Page<?> page, @RequestBody TdxCustomsRecordDTO entity) {
        try {
            return ResponseEntity
                .ok(R.ok().put("data", customsRecordService.exceptionDetail(page, entity, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("异常海关缴款书-详情列表", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 海关缴款书录入-查询列表
     *
     * @author 陈刚扬
     * @date 2020/8/6
     * @param page
     *            入参
     * @param entity
     *            入参
     * @return org.springframework.http.ResponseEntity<?>
     */
    @PostMapping("/insertQuery")
    @ResponseBody
    @SysLog("海关缴款书录入-查询列表")
    public ResponseEntity<?> insertQuery(Page<?> page, @RequestBody TdxCustomsRecordDTO entity) {
        try {
            return ResponseEntity
                .ok(R.ok().put("data", customsRecordService.insertQuery(page, entity, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("海关缴款书录入-查询列表", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 海关缴款书录入-手工录入
     *
     * @author 陈刚扬
     * @date 2020/8/6
     * @param entity
     *            入参
     * @return org.springframework.http.ResponseEntity<?>
     */
    @PostMapping("/insert")
    @ResponseBody
    @SysLog("海关缴款书录入-手工录入")
    public ResponseEntity<?> insert(@RequestBody TdxCustomsRecord entity) {
        try {
            return ResponseEntity.ok(R.ok().put("data", customsRecordService.insert(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", customsRecordService.editCustoms(entity, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("海关缴款书录入修改", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/deleteCustoms")
    @ResponseBody
    @SysLog("海关缴款书录入-删除录入")
    public ResponseEntity<?> deleteCustoms(@RequestBody TdxCustomsRecord entity) {
        try {
            return ResponseEntity
                .ok(R.ok().put("data", customsRecordService.deleteCustoms(entity, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("海关缴款书录入删除", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 海关缴款书录入-excel导入校验
     *
     * @author 陈刚扬
     * @date 2020/8/6
     * @param datalist
     *            入参
     * @return org.springframework.http.ResponseEntity<?>
     */
    @SysLog("海关缴款书录入-excel导入校验")
    @PostMapping("/importCustoms")
    public ResponseEntity<?> importCustoms(@RequestBody(required = false) String datalist) {
        try {
            Map<String, Object> pramsMap = new HashMap<>(16);

            List<String> gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            pramsMap.put("dbName", getUserInfo().getDbName());

            pramsMap.put("gfsh", gfshList);

            if (!StringHelper.isBlank(datalist)) {
                pramsMap.put("datalist", datalist);
            } else {
                return ResponseEntity.ok(R.error("请上传excel文件!"));
            }

            return ResponseEntity.ok(R.ok().put("data", customsRecordService.importCustoms(pramsMap)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 海关缴款书-excel导入提交
     *
     * @author 陈刚扬
     * @date 2020/8/6
     * @param datalist
     *            入参
     * @return org.springframework.http.ResponseEntity<?>
     */
    @SysLog("海关缴款书-excel导入提交")
    @PostMapping("/submitImportCustoms")
    public ResponseEntity<?> submitImportCustoms(@RequestBody(required = false) String datalist) {
        try {
            Map<String, Object> pramsMap = new HashMap<>(16);

            List<String> gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            pramsMap.put("dbName", getUserInfo().getDbName());

            pramsMap.put("gfsh", gfshList);

            if (!StringHelper.isBlank(datalist)) {
                pramsMap.put("datalist", datalist);
            } else {
                return ResponseEntity.ok(R.error("请上传数据"));
            }

            return ResponseEntity
                .ok(R.ok().put("data", customsRecordService.submitImportCustoms(pramsMap, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/scanCollect")
    @ResponseBody
    @SysLog("缴款书录入-扫描录入")
    public ResponseEntity<?> scanCollect(@RequestBody Map<String, Object> pramsMap) {

        if (Objects.isNull(pramsMap.get("pch"))) {

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

        try {
            return ResponseEntity.ok(R.ok().put("data", customsRecordService.scanCollect(pramsMap)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缴款书扫描录入", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 扫描录入确认
     *
     * @param datalist
     * @return
     */
    @SysLog("海关缴款书-扫描录入确认")
    @PostMapping("/submitScanCustoms")
    public ResponseEntity<?> scanCollectSubmit(@RequestBody(required = false) String datalist) {
        try {
            Map<String, Object> pramsMap = new HashMap<>(16);

            List<String> gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            pramsMap.put("dbName", getUserInfo().getDbName());

            pramsMap.put("gfsh", gfshList);

            if (!StringHelper.isBlank(datalist)) {
                pramsMap.put("datalist", datalist);
            } else {
                return ResponseEntity.ok(R.error("请上传数据"));
            }

            return ResponseEntity
                .ok(R.ok().put("data", customsRecordService.scanCollectSubmit(pramsMap, this.getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }
}
