package com.reptileclient.jlifeapp.web;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import com.reptileclient.jlifeapp.dao.hm.HmBookTradeOrderMapper;
import com.reptileclient.jlifeapp.dao.hm.HmPaymentOrderMapper;
import com.reptileclient.jlifeapp.result.ResponseInfoEnum;
import com.reptileclient.jlifeapp.result.ResultBody;
import com.reptileclient.jlifeapp.schedule.HmETLTask;
import com.reptileclient.jlifeapp.service.HmBookTradeOrderService;
import com.reptileclient.jlifeapp.service.HmEncryptPhoneRepairService;
import com.reptileclient.jlifeapp.util.DateUtils;

import com.reptileclient.jlifeapp.util.OkHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

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

/**
 * @author fuxubing
 */
@Slf4j
@RestController
@Api(value = "技师作业单管理", description = "技师作业单管理")
public class HmBookTradeOrderController {
	@Resource
	private HmBookTradeOrderService hmBookTradeOrderService;

	@Autowired
	HmETLTask task;

	@Autowired
	HmBookTradeOrderService bookTradeOrderService;

	@Resource
	HmBookTradeOrderMapper hmBookTradeOrderMapper;

	@Resource
	private HmPaymentOrderMapper paymentOrderMapper;

	@Resource
	private HmEncryptPhoneRepairService phoneRepairService;

	@ApiOperation(value = "通过预约人手机号同步盒马技师作业单")
	@GetMapping("syncHmBookTradeOrderByPhone")
	public ResultBody syncHmBookTradeOrderByPhone(String phone) {
		if (StringUtils.isBlank(phone)) {
			return new ResultBody(ResponseInfoEnum.PHONE_IS_NULL);
		}
		int row = hmBookTradeOrderService.syncHmBookTradeOrderByPhone(phone);
		return new ResultBody(row);
	}

	@ApiOperation(value = "通过服务者手机号同步当日自己的作业单")
	@GetMapping("syncHmBookTradeOrderByServantPhone")
	public ResultBody syncHmBookTradeOrderByServantPhone(String phone) {
		if (StringUtils.isBlank(phone)) {
			return new ResultBody(ResponseInfoEnum.PHONE_IS_NULL);
		}
		int row = hmBookTradeOrderService.syncHmBookTradeOrderByServantPhone(phone);
		return new ResultBody(row);
	}

	@ApiOperation(value = "同步盒马技师作业单")
	@GetMapping("addHmBookTradeOrder")
	public ResultBody addHmBookOrder() {
//        new Thread(() -> {
//            hmBookTradeOrderService.addHmBookTradeList();
//
//            /******************第四步 根据payment表 更新2个从表*******************************/
//			paymentOrderMapper.sp_update_phone_by_payment();
//
//			/********************第五步 统一修复不正确的手机号*****************************/
//			phoneRepairService.repair();
//        }).start();

		return new ResultBody();
	}

	@ApiOperation(value = "按日期范围更新盒马技师作业单，按日期范围 yyyy-mm-dd")
	@GetMapping("updateHmBookTradeOrder")
	public ResultBody updateHmBookTradeOrder(String start, String end) {
		new Thread(() -> {
			try {
				DateTime startTime = DateTime.of(DateUtils.fmtData(start));
				DateTime endTime = DateTime.of(DateUtils.fmtData(end));

				hmBookTradeOrderService.updateHmBookTradeList(startTime, endTime);

				hmBookTradeOrderMapper.deleteDistinctData(startTime);

				/****************** 第四步 根据payment表 更新2个从表 *******************************/
				paymentOrderMapper.sp_update_phone_by_payment();

				/******************** 第五步 统一修复不正确的手机号 *****************************/
				phoneRepairService.repair();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}).start();

		return new ResultBody();
	}
	
	@ApiOperation(value = "按日期范围更新盒马技师作业单，按日期范围 yyyy-mm-dd h:m:s")
	@GetMapping("updateHmBookTradeOrder2")
	public ResultBody updateHmBookTradeOrder2(String start, String end) {
		new Thread(() -> {
			try {
				DateTime startTime = DateTime.of(DateUtils.fmtData2(start));
				DateTime endTime = DateTime.of(DateUtils.fmtData2(end));

				hmBookTradeOrderService.updateHmBookTradeList(startTime, endTime);

				hmBookTradeOrderMapper.deleteDistinctData(startTime);

				/****************** 第四步 根据payment表 更新2个从表 *******************************/
				paymentOrderMapper.sp_update_phone_by_payment();

				/******************** 第五步 统一修复不正确的手机号 *****************************/
				phoneRepairService.repair();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}).start();

		return new ResultBody();
	}

	@ApiOperation(value = "开始执行同步盒马技师作业单数据")
	@GetMapping("runTaskHmBookTradeOrder")
	public ResultBody runTask() {
		new Thread(() -> {
			try {
				task.runTask();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}).start();

		return new ResultBody();
	}

	@ApiOperation(value = "服务人员打卡--我已提供服务;调用盒马api接口")
	@GetMapping("confirmBookTrade")
	public ResultBody confirmBookTrade(Integer bookTradeNo) {
		if (bookTradeNo == null || bookTradeNo == 0) {
			return new ResultBody(ResponseInfoEnum.BOOK_TRADE_NO_IS_NULL);
		}
//        HmBookTradeOrder tradeOrder = bookTradeOrderService.findByNo(bookTradeNo);
//        if (tradeOrder == null)
//            return new ResultBody(ResponseInfoEnum.BOOK_TRADE_IS_ERROR);

		Map<String, Object> result = bookTradeOrderService.confirmBookTrade(bookTradeNo);
		if (result.isEmpty() || !(boolean) result.get("success")) {
			log.info("出参：" + result.get("message"));
			return new ResultBody(400, (String) result.get("message"));
		}
		return new ResultBody(0);
	}

	@ApiOperation(value = "服务人员打卡--我已上门;调用盒马api接口")
	@GetMapping("arrivedByMerchant")
	public ResultBody arrivedByMerchant(Integer bookTradeNo) {
		if (bookTradeNo == null || bookTradeNo == 0) {
			return new ResultBody(ResponseInfoEnum.BOOK_TRADE_NO_IS_NULL);
		}
//        HmBookTradeOrder tradeOrder = bookTradeOrderService.findByNo(bookTradeNo);
//        if (tradeOrder == null)
//            return new ResultBody(ResponseInfoEnum.BOOK_TRADE_IS_ERROR);

		Map<String, Object> result = bookTradeOrderService.arrivedByMerchant(bookTradeNo);
		if (result.isEmpty() || !(boolean) result.get("success")) {
			log.info("出参：" + result.get("message"));
			return new ResultBody(400, (String) result.get("message"));
		}
		return new ResultBody(0);
	}

	public static void main(String[] args) {
//		System.out.println(DateUtil.offsetMonth(new Date(),-16));

		System.out.println(DateTime.of(DateUtils.fmtData("2020-01-01")));
	}

	@GetMapping("getHMOrderByWorkerTel")
	public Object getHMOrderByWorkerTel(@RequestParam("workerTel") String workerTel) {
		if (StringUtils.isBlank(workerTel)) {
			throw new RuntimeException(ResponseInfoEnum.PHONE_IS_NULL.getMessage());
		}
		return OkHttpUtil.getHMOrderByWorkerTel(workerTel);
	}

	@GetMapping("getOnJobByHmStationCodes")
	public Object getOnJobByHmStationCodes(@RequestParam("queryDay") String queryDay,
			@RequestParam("hmStationCodes") List<String> hmStationCodes) {
		return OkHttpUtil.getOnJobByHmStationCodes(queryDay, hmStationCodes);
	}

	@GetMapping("deleteHmBookTradeList")
	public Object deleteHmBookTradeList(String startymd, String endymd) {
		int row = 0;
		try {
			System.out.println("开始删除某日数据");
			DateTime startTime = DateTime.of(DateUtils.fmtData(startymd));
			DateTime endTime = DateTime.of(DateUtils.fmtData(endymd));
			row = hmBookTradeOrderService.deleteHmBookTradeList(startTime,endTime);
			System.out.println("删除了" + row);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultBody(row);
	}
}
