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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chushang.common.core.constant.SecurityConstants;
import com.chushang.common.core.util.StringUtils;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.datascope.annotation.DataScope;
import com.chushang.inspection.project.po.PollingTask;
import com.chushang.inspection.project.po.Template;
import com.chushang.inspection.project.service.PollingTaskService;
import com.chushang.inspection.project.service.TbTemplateService;
import com.chushang.inspection.terminal.po.Store;
import com.chushang.inspection.terminal.query.StoreQuery;
import com.chushang.inspection.terminal.service.StoreService;
import com.chushang.inspection.terminal.vo.StoreTerminalVO;
import com.chushang.inspection.terminal.vo.StoreVO;
import com.chushang.inspection.work.dto.WrkIcbcJsReceive;
import com.chushang.system.feign.RemoteDeptService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.inspection.terminal.mapper.StoreMapper;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
  * @auther: zhao
  * @date: 2024/6/21 16:39
  */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {

    @Resource
    RemoteDeptService remoteDeptService;

    @Override
    public PageResult pageList(StoreQuery query) {
        CommonParam commonParam = CommonParam.buildPageRequest();
        Page<Store> page = new Page<>(commonParam.getPage(), commonParam.getLimit());
        WrapperUtils.buildSql(query);
        List<StoreVO> storeVOS = baseMapper.pageList(query, page);
        if (CollectionUtil.isNotEmpty(storeVOS)){
            Set<Long> deptIds = storeVOS.stream().map(StoreVO::getDeptId).collect(Collectors.toSet());
            Map<Long, String> deptNameByIds = remoteDeptService.getDeptNameByIds(deptIds, SecurityConstants.INNER);
            storeVOS = storeVOS.stream().peek(s->{
                Long deptId = s.getDeptId();
                String deptName = deptNameByIds.get(deptId);
                s.setDeptName(deptName);
            }).collect(Collectors.toList());
        }
        return new PageResult(storeVOS, page);
    }

    @Override
    public void upload(MultipartFile file, Long taskId) {

    }

    /**
     * TODO 此处需要添加 任务id
     * @param dto
     * @return
     */
    @Override
    public Store save(WrkIcbcJsReceive dto) {
        Store store = getOne(new LambdaQueryWrapper<Store>()
                .eq(Store::getTaskId, "")
                .eq(Store::getStoreNo, dto.getMerId())
        );
        if (store == null) {
            store = new Store().save(dto);
            save(store);
        }
        return store;
    }

}
