package cn.superdesk.uniorder.controller.order._leaveover;

import cn.superdesk.uniorder.common.constants.ResponseMsgConstants;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.order.BankRecord;
import cn.superdesk.uniorder.dubbo.consumer.order._leaveover.BankRecordDubboConsumerService;
import cn.superdesk.uniorder.common.HttpClientUtils;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import com.alibaba.fastjson2.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 表都不存在了
 * @Author: linjinyu
 * @CreateDate: 2021/7/23 下午12:05
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/7/23 下午12:05
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Deprecated
@Slf4j
@RestController
@RequestMapping("/bankRecord")
public class BankRecordController extends BaseController {

	@Autowired
	private BankRecordDubboConsumerService bankRecordDubboConsumerService;

	/**
	 * 返回角色列表-分页
	 *
	 * @return
	 */
	@PostMapping("/findByPage")
	@ResponseBody
	public R<List<BankRecord>> findByPage() {

		R<List<BankRecord>> result = new R<>();

		try {
			Map<String, Object> params = new HashMap<>();

			if (StringUtils.isNotEmpty(getString("offStatus"))) {
				params.put("offStatus", Byte.valueOf(getString("offStatus")));
			}

			params.put("bankName", getString("bankName"));
			params.put("startTime", getString("startTime"));
			params.put("endTime", getString("endTime"));

			params.put("payName", getString("payName"));
			params.put("bankNo", getString("bankNo"));
			params.put("sn", getString("sn"));

			result = bankRecordDubboConsumerService.findAllByPage(getToken(), params, getPageNum(), getPageSize());

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage("系统查找失败！");

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();

			e.printStackTrace();
		}

		return result;
	}

	/**
	 * 返回银行列表
	 *
	 * @return
	 */
	@PostMapping("/findBank")
	@ResponseBody
	public R<List<String>> findBank() {

		R<List<String>> result = new R<>();

		try {

			List<String> ls = bankRecordDubboConsumerService.findBank();

			result.setData(ls);
			result.setSuccess(true);

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage("系统查找失败！");

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();

			e.printStackTrace();
		}
		return result;
	}


	/**
	 * 新增修改
	 *
	 * @param role
	 * @return
	 */
	@PostMapping("/impdata")
	@ResponseBody
	public R<List<BankRecord>> impdata() {

		R<List<BankRecord>> result = new R<>();

		try {

			String instr = HttpClientUtils.readInputStream(request.getInputStream(), Charset.forName("utf-8"));

			if (StringUtils.isEmpty(instr)) {
				result.setSuccess(false);
				result.setMessage("上传文件数据为空。");
				return result;
			}

			JSONArray userArray = JSONArray.parseArray(instr);
			if (userArray == null) {
				result.setSuccess(false);
				result.setMessage("上传文件数据格式错误。");
				return result;
			}

			List<BankRecord> list = userArray.toJavaList(BankRecord.class);
			if (list == null || list.size() == 0) {
				result.setSuccess(false);
				result.setMessage("上传文件无数据或格式错误。");
				return result;
			}

			//int count=list.size();//上传总数
			List<BankRecord> errPros = new ArrayList<>();
			for (BankRecord item : list) {
				try {

					Boolean res = bankRecordDubboConsumerService.addObj(getToken(), item);
					if (!res) {
						errPros.add(item);
					}
				} catch (Exception e) {

					errPros.add(item);
				}
			}
			result.setData(errPros);

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();

			e.printStackTrace();
		}

		return result;
	}
}
