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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.DaoTemplate;
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.util.StringUtils;
import com.chushang.common.core.web.Result;
import com.chushang.common.dict.feign.RemoteDictDataService;
import com.chushang.common.dict.utils.DictUtils;
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.project.dto.ProjectDTO;
import com.chushang.inspection.project.dto.TaskQuery;
import com.chushang.inspection.project.po.PollingTask;
import com.chushang.inspection.project.po.WrkProject;
import com.chushang.inspection.project.service.PollingTaskService;
import com.chushang.inspection.project.vo.ConfigVO;
import com.chushang.inspection.project.vo.TaskVO;
import com.chushang.inspection.terminal.dto.StoreImportDTO;
import com.chushang.inspection.terminal.mapper.StoreImportMapper;
import com.chushang.inspection.terminal.mapper.StoreMapper;
import com.chushang.inspection.terminal.mapper.TerminalInsMapper;
import com.chushang.inspection.terminal.mapper.TerminalMapper;
import com.chushang.inspection.terminal.po.Store;
import com.chushang.inspection.terminal.po.StoreImport;
import com.chushang.inspection.terminal.po.Terminal;
import com.chushang.inspection.terminal.po.TerminalIns;
import com.chushang.inspection.terminal.query.StoreImportQuery;
import com.chushang.inspection.terminal.query.StoreQuery;
import com.chushang.inspection.terminal.service.StoreImportService;
import com.chushang.inspection.terminal.vo.StoreVO;
import com.chushang.inspection.terminal.vo.TerminalVO;
import com.chushang.inspection.utils.BeanCopyUtils;
import com.chushang.inspection.work.po.WrkImg;
import com.chushang.inspection.work.vo.WrkImgVO;
import com.chushang.security.utils.SecurityUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @auther: zhao
 * @date: 2024/7/3 9:56
 */
@Slf4j
@Service
public class StoreImportServiceImpl extends ServiceImpl<StoreImportMapper, StoreImport> implements StoreImportService {


    @Resource
    TerminalMapper terminalMapper;

    @Resource
    TerminalInsMapper terminalInsMapper;


    @Resource
    StoreMapper storeMapper;

    @Resource
    PollingTaskService pollingTaskService;

    @Resource
    RemoteDictDataService remoteDictDataService;


    @Override
    public PageResult pageList(StoreImportQuery query, CommonParam commonParam) {
        LambdaQueryWrapper<StoreImport> queryWrapper = WrapperUtils.builder(query, commonParam);
        IPage<StoreImport> page = this.page(
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(commonParam.getPage(), commonParam.getLimit()),
                queryWrapper
        );

        return new PageResult(page);
    }


    public Store genStoreEntity(StoreImportDTO storeImportDTO) {
        // 通过任务名称查询任务
        List<PollingTask> list = pollingTaskService.list(new LambdaQueryWrapper<PollingTask>().eq(PollingTask::getName, storeImportDTO.getTaskName()));
        if (CollectionUtil.isEmpty(list)) {
            throw new RuntimeException("任务名称不存在");
        }
        PollingTask topTask = pollingTaskService.getTopTask(list.get(0).getId());
        Store store = new Store();
        store.setAccountManager(storeImportDTO.getAccountManager());
        store.setAccountPhone(storeImportDTO.getAccountPhone());
        store.setLegalName(storeImportDTO.getLegalName());
        store.setSpecialNum(storeImportDTO.getSpecialNum());
        store.setShopName(storeImportDTO.getShopName());
        store.setProducts(storeImportDTO.getProducts());
        store.setTipTool(storeImportDTO.getTipTool());
        LocalDate registerDate = storeImportDTO.getRegisterTime();
        LocalDateTime registerTime = LocalDateTime.of(registerDate, LocalTime.MIDNIGHT);
        store.setInsFre(null != storeImportDTO.getInsFre() ? Integer.valueOf(storeImportDTO.getInsFre()) : null);
        store.setStoreNo(storeImportDTO.getStoreNo());
        store.setStoreName(storeImportDTO.getStoreName());
        store.setStoreAddress(storeImportDTO.getStoreAddress());
        store.setStoreContact(storeImportDTO.getStoreContact());
        store.setStorePhone(storeImportDTO.getStorePhone());
        store.setStoreType(null != storeImportDTO.getStoreType() ? Integer.valueOf(storeImportDTO.getStoreType()) : null);
        store.setRegisterTime(registerTime); // registertime
        store.setTaskId(topTask.getId());
        store.setDeptId(SecurityUtils.getDeptId());
        store.setLowerTaskId(list.get(0).getId());
        store.setStoreId(IdUtil.getSnowflake().nextId());
        store.setDelState(false);
        store.setProjectId(topTask.getProjectId());
        return store;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int confimImport(List<StoreImportDTO> lists) {
        int i = 0;
        // 商户类型枚举
        for (StoreImportDTO storeImportDTO : lists) {
            i++;
            if (StringUtils.isBlank(storeImportDTO.getTaskName())) {
                throw new RuntimeException("任务名称不能为空");
            }

            Store store = genStoreEntity(storeImportDTO);
            storeMapper.insert(store);
            Terminal terminal = new Terminal();

            terminal.setProjectId(store.getProjectId());
            terminal.setTerminalNo(storeImportDTO.getTerminalNo());
            terminal.setTerminalSn(storeImportDTO.getTerminalSn());
            terminal.setTerminalModel(storeImportDTO.getTerminalModel());

            terminal.setTerminalType(null != storeImportDTO.getTerminalType() ? Integer.valueOf(storeImportDTO.getTerminalType()) : null);//storeImportDTO.getTerminalType()

            terminal.setTerminalVersion(storeImportDTO.getTerminalVersion());
            terminal.setTerminalSource(null != storeImportDTO.getTerminalSource() ? Integer.valueOf(storeImportDTO.getTerminalSource()) : null);//storeImportDTO.getTerminalSource()
            terminal.setTerminalAddress(storeImportDTO.getTerminalAddress());
            terminal.setOccupy(0);
            terminal.setTerminalStatus(1);
            terminal.setStoreId(store.getStoreId());
            terminal.setLowerTaskId(store.getLowerTaskId());
            terminal.setTaskId(store.getTaskId());
            terminal.setDeptId(SecurityUtils.getDeptId());
            terminalMapper.insert(terminal);
            TerminalIns terminalIns = new TerminalIns();
//            BeanCopyUtils.copy(storeImportDTO, TerminalIns.class);
            terminalIns.setTerminalId(terminal.getTerminalId());
            terminalInsMapper.insert(terminalIns);
        }
        return i;
    }


}
