package com.gic.haoban.manage.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.haoban.base.api.common.BasePageInfo;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.api.dto.PreDealLogInfoDTO;
import com.gic.haoban.manage.api.dto.SyncCheckDTO;
import com.gic.haoban.manage.api.dto.SyncTaskDTO;
import com.gic.haoban.manage.api.service.DealSyncOperationApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.qo.SyncDealQO;
import com.gic.redis.data.util.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class SyncDealContoller extends WebBaseController {

    private static final Logger logger = LoggerFactory.getLogger(SyncDealContoller.class);

    @Autowired
    private DealSyncOperationApiService dealSyncOperationApiService;


    @RequestMapping("/test/department-sync")
    @ResponseBody
    public HaobanResponse departmentBatchSyncTest(@RequestBody SyncDealQO qo, String wxEid) {
        String task = null;
        if (StringUtils.isNotBlank(wxEid)) {
            task = dealSyncOperationApiService.createTask(wxEid, "门店同步", "11111", "部门同步");
            dealSyncOperationApiService.dealDepartment(task, wxEid, qo.getAddDepartment(), qo.getEditDepartment());
        } else {
            WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
            String wxEnterpriseId = login.getWxEnterpriseId();
            String key = "haoban-sync-department-" + wxEnterpriseId;
            if (RedisUtil.getCache(key) != null) {
                return resultResponse(HaoBanErrCode.ERR_10011);
            }
            task = dealSyncOperationApiService.createTask(wxEnterpriseId, "门店同步", login.getClerkId(), "部门同步");
            dealSyncOperationApiService.dealDepartment(task, login.getWxEnterpriseId(), qo.getAddDepartment(), qo.getEditDepartment());
        }

        return resultResponse(HaoBanErrCode.ERR_1, task);

    }

    @RequestMapping("/department-sync")
    @ResponseBody
    public HaobanResponse departmentBatchSync(@RequestBody SyncDealQO qo) {
        String task = null;

        WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = login.getWxEnterpriseId();

        logger.info("login:{}", JSONObject.toJSONString(login));
        logger.info("data:{}", JSONObject.toJSONString(qo));

        String taskLock = dealSyncOperationApiService.getTaskLock(wxEnterpriseId);
        if (null != taskLock) {
            return resultResponse(HaoBanErrCode.ERR_10011);
        }
        String staffId = "admin";
        String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        task = dealSyncOperationApiService.createTask(wxEnterpriseId, "门店同步-" + format, staffId, "部门同步");
        if (task == null) {
            return resultResponse(HaoBanErrCode.ERR_10011);
        }
        dealSyncOperationApiService.dealDepartment(task, login.getWxEnterpriseId(), qo.getAddDepartment(), qo.getEditDepartment());

        return resultResponse(HaoBanErrCode.ERR_1, task);

    }

    /**
     * 同步状态校验
     *
     * @param syncKey
     * @return
     */
    @RequestMapping("department-sync-check")
    public HaobanResponse syncSuccess(String wxEnterpriseId) {
        if (StringUtils.isBlank(wxEnterpriseId)) {
            WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
            wxEnterpriseId = login.getWxEnterpriseId();
        }
        SyncCheckDTO taskCheck = dealSyncOperationApiService.getTaskCheck(wxEnterpriseId);
        return resultResponse(HaoBanErrCode.ERR_1, taskCheck);
    }

    /**
     * 同步状态校验
     *
     * @param syncKey
     * @return
     */
    @RequestMapping("/test/department-sync-check")
    public HaobanResponse testSyncSuccess(String wxEnterpriseId) {
        if (StringUtils.isBlank(wxEnterpriseId)) {
            WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
            wxEnterpriseId = login.getWxEnterpriseId();
        }
        SyncCheckDTO taskCheck = dealSyncOperationApiService.getTaskCheck(wxEnterpriseId);
        return resultResponse(HaoBanErrCode.ERR_1, taskCheck);
    }

    @RequestMapping("/re-sync")
    @ResponseBody
    public HaobanResponse clerkReSync(String taskId) {
        if (StringUtils.isBlank(taskId)) {
            WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
            String wxEnterpriseId = login.getWxEnterpriseId();
            taskId = dealSyncOperationApiService.getTaskLock(wxEnterpriseId);
        }
        dealSyncOperationApiService.reDealDepartment(taskId, -1);
        return resultResponse(HaoBanErrCode.ERR_1);

    }

    @RequestMapping("/close-sync")
    @ResponseBody
    public HaobanResponse closeReSync(String wxEnterpriseId) {
        if (StringUtils.isBlank(wxEnterpriseId)) {
            WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
            wxEnterpriseId = login.getWxEnterpriseId();
        }
        dealSyncOperationApiService.closeTask(wxEnterpriseId);
        return resultResponse(HaoBanErrCode.ERR_1);

    }

    /**
     * 同步日志列表
     *
     * @param wxEnterpriseId
     * @param pageInfo
     * @return
     */
    @RequestMapping("/sync-task")
    @ResponseBody
    public HaobanResponse syncTask(String wxEnterpriseId, BasePageInfo pageInfo) {
        if (StringUtils.isBlank(wxEnterpriseId)) {
            WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
            wxEnterpriseId = login.getWxEnterpriseId();
        }
        Page<SyncTaskDTO> retPage = dealSyncOperationApiService.listSyncTask(wxEnterpriseId, pageInfo);
        return resultResponse(HaoBanErrCode.ERR_1, retPage);

    }

    /**
     * 同步日志详情
     *
     * @param wxEnterpriseId
     * @param pageInfo
     * @return
     */
    @RequestMapping("/sync-task-detail")
    @ResponseBody
    public HaobanResponse syncTaskDetail(String wxEnterpriseId, String taskId, BasePageInfo pageInfo) {
        if (StringUtils.isBlank(wxEnterpriseId)) {
            WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
            wxEnterpriseId = login.getWxEnterpriseId();
        }
        Page<PreDealLogInfoDTO> retPage = dealSyncOperationApiService.listSyncTaskDetail(wxEnterpriseId, taskId, pageInfo);
        return resultResponse(HaoBanErrCode.ERR_1, retPage);

    }

    /**
     * 同步日志详情
     *
     * @return
     */
    @RequestMapping("/get-fresh-on-off")
    @ResponseBody
    public HaobanResponse getfreshonoff() {

        return resultResponse(HaoBanErrCode.ERR_1);

    }
}
