package com.reptileclient.jlifeapp.web;

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.HmEncryptPhoneRepairService;
import com.reptileclient.jlifeapp.service.HmPaymentOrderService;
import com.reptileclient.jlifeapp.util.DateUtils;

import cn.hutool.core.date.DateTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.RestController;

import javax.annotation.Resource;

/**
 * @author fuxubing
 */
@RestController
@Api(value = "服务付款单管理", description = "服务付款单管理")
public class HmPaymentOrderController {
    @Resource
    private HmPaymentOrderService hmPaymentOrderService;

    @Autowired
    HmETLTask task;

    @Resource
	private HmPaymentOrderMapper paymentOrderMapper;
    
    @Resource
	private HmEncryptPhoneRepairService phoneRepairService;
    
    @ApiOperation(value = "通过用户手机号同步盒马服务付款单")
    @GetMapping("syncHmPaymentOrderByPhone")
    public ResultBody syncHmPaymentOrderByPhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            return new ResultBody(ResponseInfoEnum.PHONE_IS_NULL);
        }
        int row = hmPaymentOrderService.syncHmPaymentOrderByPhone(phone);
        return new ResultBody(row);
    }

    @ApiOperation(value = "同步盒马服务付款单")
    @GetMapping("addHmPaymentOrder")
    public ResultBody addHmPaymentOrder(String startdate) throws InterruptedException {
    	DateTime startTime =DateTime.of(DateUtils.fmtData(startdate));
        new Thread(() -> {
           // hmPaymentOrderService.addPaymentOrderList(startTime);
        }).start();
        return new ResultBody();
    }

    @ApiOperation(value = "更新盒马服务付款单")
    @GetMapping("updateHmPaymentOrder")
    public ResultBody updateHmPaymentOrder() {
        new Thread(() -> {
            hmPaymentOrderService.updatePaymentOrderList();
        }).start();
        return new ResultBody();
    }

    @ApiOperation(value = "更新盒马服务付款单按时间 年月日")
    @GetMapping("updateHmPaymentOrder2")
    public ResultBody updateHmPaymentOrder2(String start,String end) {
    	  DateTime startTime = DateTime.of(DateUtils.fmtData(start));
          DateTime endTime = DateTime.of(DateUtils.fmtData(end));
        new Thread(() -> {
            hmPaymentOrderService.updatePaymentOrderList2(startTime,endTime);
            hmPaymentOrderService.deleteDistinctData(startTime);
            
        	/******************第四步 根据payment表 更新2个从表*******************************/
			paymentOrderMapper.sp_update_phone_by_payment();

			/********************第五步 统一修复不正确的手机号*****************************/
			phoneRepairService.repair();
        }).start();
        return new ResultBody();
    }

    @ApiOperation(value = "全量修复付款单状态为：已取消")
    @GetMapping("repairPaymentOrderState")
    public void repairPaymentOrderState(){
        try {
            hmPaymentOrderService.updatePaymentOrderCancelOrder();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "查15天付款单年月日")
    @GetMapping("test15day")
    public ResultBody test15day(String start,String end) {
    	  DateTime startTime = DateTime.of(DateUtils.fmtData(start));
          DateTime endTime = DateTime.of(DateUtils.fmtData(end));
        new Thread(() -> {
            hmPaymentOrderService.update15(startTime,endTime);
            //hmPaymentOrderService.deleteDistinctData(startTime);
        }).start();
        return new ResultBody();
    }


    @ApiOperation(value = "1点的盒马支付单")
    @GetMapping("taskHmPaymentOrder")
    public ResultBody taskHmPaymentOrder() {
        new Thread(() -> {
        	//task.runTask();
        }).start();
        return new ResultBody();
    }


}
