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

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.DateUtil;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.manage.web.qo.offline.PageOfflinePreQO;
import com.gic.haoban.manage.web.qo.offline.WriteOffOfflinePreQO;
import com.gic.haoban.manage.web.vo.offline.CountOfflinePreVO;
import com.gic.haoban.manage.web.vo.offline.OfflinePreInfoVO;
import com.gic.marketing.pro.api.dto.offline.CountOfflinePreDTO;
import com.gic.marketing.pro.api.dto.offline.OfflinePreInfoDTO;
import com.gic.marketing.pro.api.qdto.offline.PageOfflinePreQDTO;
import com.gic.marketing.pro.api.qdto.offline.WriteOffOfflinePreQDTO;
import com.gic.marketing.pro.api.service.offline.OfflinePreApiService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 好办小程序-线下预约
 */
@Controller
public class OfflinePreController {
    private final Logger logger = LogManager.getLogger(this.getClass());
    @Autowired
    private OfflinePreApiService offlinePreApiService;

    /**
     * 预约列表
     * @param qo 参数
     * @return 预约列表
     */
    @RequestMapping("/page-offline-pre")
    @ResponseBody
    public RestResponse<Page<OfflinePreInfoVO>> pageOfflinePre(@RequestBody PageOfflinePreQO qo){
        ServiceResponse<Page<OfflinePreInfoDTO>> resp = offlinePreApiService.pageOfflinePre(JSONObject.parseObject(JSONObject.toJSONString(qo), PageOfflinePreQDTO.class));
        Page<OfflinePreInfoVO> pageVO = new Page<>() ;
        pageVO.setCurrentPage(resp.getResult().getCurrentPage());
        pageVO.setTotalCount(resp.getResult().getTotalCount());
        pageVO.setPageSize(resp.getResult().getPageSize());
        if(null != resp.getResult() && null != resp.getResult().getResult()) {
            List<OfflinePreInfoDTO> list = resp.getResult().getResult() ;
            List<OfflinePreInfoVO> voList = new ArrayList<>() ;
            for(OfflinePreInfoDTO dto : list) {
                OfflinePreInfoVO vo = EntityUtil.changeEntityByJSON(OfflinePreInfoVO.class,dto) ;
                if(null != qo.getType()) {
                    if(qo.getType()==1) {
                        if ((dto.getPreTime().getTime() < System.currentTimeMillis() || this.getAheadSignFlag(dto.getAheadSignFlag()) == 1)
                                && DateUtil.getStartTimeOfDay(dto.getPreTime()).getTime() >= DateUtil.getStartTimeOfDay().getTime()) {
                            vo.setCanSign(1);
                        } else {
                            if (DateUtil.getStartTimeOfDay(dto.getPreTime()).getTime() < DateUtil.getStartTimeOfDay().getTime()) {
                                logger.info("已过期");
                                continue;
                            } else if (this.getAheadSignFlag(dto.getAheadSignFlag()) == 0 && dto.getPreTime().getTime() > System.currentTimeMillis()) {
                                vo.setReason("不可提前签到");
                            }
                        }
                    }
                    if(qo.getType()==2) {
                        if(dto.getStatusFlag() ==0) {
                            vo.setStatusFlag(2);
                        }
                    }
                }
                voList.add(vo) ;
            }
            pageVO.setResult(voList);
        }
        return RestResponse.successResult(pageVO) ;
    }
    private int getAheadSignFlag(Integer flag) {
        if(null == flag) {
            return  1 ;
        }
        return flag ;
    }
    /**
     * 获取预约详情
     * @param offlinePreId 预约ID
     * @param enterpriseId 企业ID
     * @return 预约详情
     */
    @RequestMapping("/get-offline-pre")
    @ResponseBody
    public RestResponse<OfflinePreInfoVO> getOfflinePre(Long offlinePreId, String enterpriseId){
        ServiceResponse<OfflinePreInfoDTO> offlinePreInfo = offlinePreApiService.getOfflinePre(offlinePreId, enterpriseId, null);
        return JSONObject.parseObject(JSONObject.toJSONString(offlinePreInfo, SerializerFeature.WriteMapNullValue), RestResponse.class);
    }

    /**
     * 核销预约单
     * @param qo 入参
     * @return
     */
    @RequestMapping("/write-off-offline-pre")
    @ResponseBody
    public RestResponse<Boolean> writeOffOfflinePre(@RequestBody WriteOffOfflinePreQO qo){
        ServiceResponse<Boolean> writeOffOfflinePre = offlinePreApiService.writeOffOfflinePre(JSONObject.parseObject(JSONObject.toJSONString(qo), WriteOffOfflinePreQDTO.class));
        return JSONObject.parseObject(JSONObject.toJSONString(writeOffOfflinePre, SerializerFeature.WriteMapNullValue), RestResponse.class);
    }

    /**
     * 预约统计
     * @param storeId 门店ID
     * @param enterpriseId 企业ID
     * @return 预约详情
     */
    @RequestMapping("/count-offline-pre")
    @ResponseBody
    public RestResponse<CountOfflinePreVO> countOfflinePre(String storeId, String enterpriseId){
        ServiceResponse<CountOfflinePreDTO> countOfflinePre = offlinePreApiService.countOfflinePre(storeId, enterpriseId);
        return JSONObject.parseObject(JSONObject.toJSONString(countOfflinePre, SerializerFeature.WriteMapNullValue), RestResponse.class);
    }


    /**
     * 校验是否核销
     * @param offlinePreId 预约ID
     * @param storeId 门店ID
     * @return 预约详情
     */
    @RequestMapping("/check-offline-pre-write-off")
    @ResponseBody
    public RestResponse<Void> checkOfflinePreWriteOff(String offlinePreId, String storeId){
        if(StringUtils.isBlank(offlinePreId) || !offlinePreId.trim().chars().allMatch(Character::isDigit)) {
            return RestResponse.failure("9999","参数错误") ;
        }
        ServiceResponse<Void> countOfflinePre = offlinePreApiService.checkOfflinePreWriteOff(Long.valueOf(offlinePreId.trim()), storeId);
        return JSONObject.parseObject(JSONObject.toJSONString(countOfflinePre, SerializerFeature.WriteMapNullValue), RestResponse.class);
    }
}
