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

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.dsmongo.api.service.MongoOperationService;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.manage.web.qo.logrecord.LogRecordQo;
import com.gic.haoban.manage.web.vo.logrecord.LogRecordTypeVO;
import com.gic.log.record.bean.OptRecordLog;
import com.gic.log.record.bean.OptRecordLogHaoban;
import com.gic.log.record.enums.OrderEnum;
import com.gic.log.record.service.LogSearchApiService;
import com.gic.log.record.util.GicLogRecordCategoryEnum;
import com.gic.log.record.util.GicLogRecordOptTypeEnum;
import com.gic.log.record.util.LogQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/log-record")
public class HbLogRecordController {
    
    private static final Logger LOGGER = LogManager.getLogger(HbLogRecordController.class);
    @Autowired
    MongoOperationService mongoOperationService;
    @Autowired
    private LogSearchApiService logSearchApiService;

    /**
     * 系统类型 0gic 1好办
     */
    private static Integer systemType = 1;

    @RequestMapping("/list")
    @ResponseBody
    public RestResponse pageLog(LogRecordQo qo, @RequestParam(defaultValue="1")int pageNum  , @RequestParam(defaultValue="20")int pageSize){
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String eid = qo.getEnterpriseId() ;
        if(null != loginUser && StringUtils.isEmpty(eid)) {
            eid = loginUser.getEnterpriseId() ;
        }
        //使用lambda拼装查询表达式
        LogQueryWrapper<OptRecordLogHaoban> queryWrapper = new LogQueryWrapper<>();
        queryWrapper.eq(OptRecordLogHaoban::getEnterpriseId, eid).eq(OptRecordLogHaoban::getStatusFlag, 1);
        queryWrapper.eq(OptRecordLogHaoban::getWxEnterpriseId, loginUser.getWxEnterpriseId());
        if (StringUtils.isNotBlank(qo.getBusinessType())) {
            queryWrapper.eq(OptRecordLogHaoban::getBusinessType, qo.getBusinessType());
        }
        if (StringUtils.isNotBlank(qo.getOptType())) {
            queryWrapper.eq(OptRecordLogHaoban::getOptType, qo.getOptType());
        }
        if (StringUtils.isNoneBlank(qo.getStartDate(),qo.getEndDate())) {
            String endDate = qo.getEndDate() + " 23:59:59";
            String startDate = qo.getStartDate() + " 00:00:00";
            try {
                Date startDateD = DateUtils.parseDate(startDate, "yyyy-MM-dd HH:mm:ss");
                Date endDateD = DateUtils.parseDate(endDate, "yyyy-MM-dd HH:mm:ss");
                queryWrapper.between(OptRecordLogHaoban::getCreateTime,startDateD,endDateD);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(null == loginUser || StringUtils.isBlank(loginUser.getOperationUserName())) {
            //历史数据会刷成0.
            queryWrapper.eq(OptRecordLogHaoban::getYwFlag, 0);
        }
        if (StringUtils.isNotBlank(qo.getSearch())) {
            queryWrapper.like(OptRecordLogHaoban::getContent, qo.getSearch());
        }
        if (StringUtils.isNotBlank(qo.getOptUser())) {
            queryWrapper.eq(OptRecordLogHaoban::getUserId, qo.getOptUser());
        }
        //0:gic 1:好办 2：运维
        queryWrapper.eq(OptRecordLogHaoban::getSystemType, systemType);
        queryWrapper.orderBy(OptRecordLogHaoban::getCreateTime, OrderEnum.DESC);

        BasePageInfo basePageInfo = new BasePageInfo();
        basePageInfo.setPageNum(pageNum);
        basePageInfo.setPageSize(pageSize);
        //查询接口 分页方式
        ServiceResponse<Page> pageServiceResponse = logSearchApiService.queryByPage(queryWrapper, basePageInfo);
        return RestResponse.successResult(pageServiceResponse.getResult());
    }

    /**
     * 获取 业务模块
     * @return
     */
    @RequestMapping("business-types")
    @ResponseBody
    public RestResponse businessTypes(){
        List<LogRecordTypeVO> ret=new ArrayList<>();
        GicLogRecordCategoryEnum[] values = GicLogRecordCategoryEnum.values();
        for (GicLogRecordCategoryEnum value : values) {
            if (Objects.equals(value.getSystemType(), systemType)) {
                LogRecordTypeVO mid = new LogRecordTypeVO();
                mid.setKey(value.getType());
                mid.setValue(value.getName());
                ret.add(mid);
            }
        }
        return RestResponse.successResult(ret);
    }

    /**
     * 获取 操作类型
     * @return
     */
    @RequestMapping("opt-types")
    @ResponseBody
    public RestResponse optTypes(){
        List<LogRecordTypeVO> ret=new ArrayList<>();
        GicLogRecordOptTypeEnum[] values = GicLogRecordOptTypeEnum.values();
        for (GicLogRecordOptTypeEnum value : values) {
            if (Objects.equals(value.getSystemType(), systemType)) {
                LogRecordTypeVO mid = new LogRecordTypeVO();
                mid.setKey(value.getType());
                mid.setValue(value.getName());
                ret.add(mid);
            }
        }
        return RestResponse.successResult(ret);
    }


}
