package com.chushang.inspection.work.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.common.core.constant.SecurityConstants;
import com.chushang.common.core.exception.ResultException;
import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.core.util.DateUtils;
import com.chushang.common.core.util.IPUtils;
import com.chushang.common.core.util.SpringUtils;
import com.chushang.common.core.web.Result;
import com.chushang.common.dict.utils.DictUtils;
import com.chushang.common.excel.utils.ExcelUtils;
import com.chushang.common.mybatis.enums.Operator;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.inspection.ins.GeneratedInsFactory;
import com.chushang.inspection.project.dto.AuditDTO;
import com.chushang.inspection.project.po.InspectionData;
import com.chushang.inspection.project.po.WrkProject;
import com.chushang.inspection.project.service.InspectionDataService;
import com.chushang.inspection.project.service.PollingTaskService;
import com.chushang.inspection.terminal.po.FiveStore;
import com.chushang.inspection.terminal.po.Store;
import com.chushang.inspection.terminal.po.Terminal;
import com.chushang.inspection.terminal.po.TerminalIns;
import com.chushang.inspection.terminal.service.FiveStoreService;
import com.chushang.inspection.terminal.service.StoreService;
import com.chushang.inspection.terminal.service.TerminalInsService;
import com.chushang.inspection.terminal.service.TerminalService;
import com.chushang.inspection.terminal.vo.FiveStoreVO;
import com.chushang.inspection.utils.PoolUtils;
import com.chushang.inspection.utils.StreamUtils;
import com.chushang.inspection.utils.TaskConfigUtils;
import com.chushang.inspection.work.dto.DispatchDTO;
import com.chushang.inspection.work.dto.WrkInfoDTO;
import com.chushang.inspection.work.enums.WorkTypeOperation;
import com.chushang.inspection.work.mapper.WrkAuditRuleMapper;
import com.chushang.inspection.work.mapper.WrkInfoMapper;
import com.chushang.inspection.work.po.*;
import com.chushang.inspection.work.query.*;
import com.chushang.inspection.work.service.*;
import com.chushang.inspection.work.vo.*;
import com.chushang.security.entity.po.SysUser;
import com.chushang.security.utils.SecurityUtils;
import com.chushang.system.feign.RemoteUserService;
import com.chushang.task.entity.dto.CreateTaskDTO;
import com.chushang.task.enums.ServiceEnum;
import com.chushang.task.enums.TaskTypeEnum;
import com.chushang.task.feign.RemoteTaskService;
import lombok.RequiredArgsConstructor;
import org.bouncycastle.util.Strings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @auther: zhao
 * @date: 2024/6/26 17:13
 */
@Service
@RequiredArgsConstructor
public class WrkAuditRuleServiceImpl extends ServiceImpl<WrkAuditRuleMapper, WrkAuditRule> implements WrkAuditRuleService {

    @Override
    public PageResult queryRulePage(WrkAuditRuleQuery query) {
        Map<String,String> date = new HashMap<>();
        date.put("1","周一");
        date.put("2","周二");
        date.put("3","周三");
        date.put("4","周四");
        date.put("5","周五");
        date.put("6","周六");
        date.put("7","周日");
        CommonParam commonParam = CommonParam.buildPageRequest();
        LambdaQueryWrapper<WrkAuditRule> queryWrapper = WrapperUtils.builder(query, commonParam);
        IPage<WrkAuditRule> page = this.page(
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(commonParam.getPage(), commonParam.getLimit()),
                queryWrapper
        );
        for (WrkAuditRule record : page.getRecords()) {
            List<String> list = new ArrayList<>();
            for (String s : record.getRuleTime().split(",")) {
                list.add(date.get(s));
            }
            record.setRuleTimeList(Arrays.asList(record.getRuleTime().split(",")));
            record.setRuleTime(String.join(",", list));

        }

        return new PageResult(page);
    }

    @Override
    public int saveOrUpdate(WrkAuditRuleDTO dto) {
        if (ObjectUtil.isNotNull(dto.getId())) {
            WrkAuditRule wrkAuditRule = this.getById(dto.getId());
            Assert.notNull(wrkAuditRule, "修改失败，数据不存在");
            BeanUtil.copyProperties(dto, wrkAuditRule);
            if(!dto.getRuleTimeList().isEmpty()){
                wrkAuditRule.setRuleTime(String.join(",", dto.getRuleTimeList()));
            }
            return this.updateById(wrkAuditRule) ? 1 : 0;
        } else {
            WrkAuditRule wrkAuditRule = new WrkAuditRule();
            BeanUtil.copyProperties(dto, wrkAuditRule);
            wrkAuditRule.setId(IdUtil.getSnowflakeNextId());
            wrkAuditRule.setDeptId(SecurityUtils.getDeptId());
            wrkAuditRule.setUserId(SecurityUtils.getUserId());
            wrkAuditRule.setRuleTime(String.join(",", dto.getRuleTimeList()));
            return this.save(wrkAuditRule) ? 1 : 0;
        }
    }

}
