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

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
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.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.inspection.project.vo.CountVO;
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.StoreVO;
import com.chushang.inspection.work.dto.WrkIcbcJsReceive;
import com.chushang.inspection.work.po.WrkIcbcJs;
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 saveJcIcbc(WrkIcbcJsReceive dto) {
        List<Store> stores = list(new LambdaQueryWrapper<Store>()
                .eq(Store::getTaskId, 1701151817643495425L)
                .eq(Store::getStoreNo, dto.getMerId())
        );
        Store store;
        if (CollectionUtil.isEmpty(stores)){
            store = new Store().saveJcIcbc(dto);
            save(store);
        }
        // 如果不为空, 不需要重新创建一条数据
        else {
            store = stores.stream()
                    .filter(s -> StringUtil.isNotEmpty(s.getStoreAddress())
                            && s.getStoreAddress().equals(dto.getMerAddress()))
                    .collect(Collectors.toSet()).stream().findFirst().orElse(null);
            if (store != null){
                store = new Store().saveJcIcbc(dto);
                save(store);
            }
            // 判断两个地址是否相同, 不同的话, 重新入库一条商户id
//            String storeAddress = store.getStoreAddress();
//            String merAddress = dto.getMerAddress();
//            if (StringUtil.isNotEmpty(storeAddress) && StringUtil.isNotEmpty(merAddress) &&
//                    storeAddress.equals(merAddress)){
//
//            }
        }
        return store;
    }

    @Override
    public List<CountVO> countByProjectIds(Set<Long> projectIds) {
        return baseMapper.countByProjectIds(projectIds);
    }

    @Override
    public Store saveJcIcbc(WrkIcbcJs w) {
        List<Store> stores = list(new LambdaQueryWrapper<Store>()
                .eq(Store::getTaskId, 1701151817643495425L)
                .eq(Store::getStoreNo, w.getMerId())
        );
        Store store;
        if (CollectionUtil.isEmpty(stores)){
            store = new Store().saveJcIcbc(w);
            save(store);
        }
        // 如果不为空, 不需要重新创建一条数据
        else {
            store = stores.stream()
                    .filter(s -> s.getStoreAddress().equals(w.getMerAddress()))
                    .collect(Collectors.toSet()).stream().findFirst().orElse(null);
            if (store == null){
                store = new Store().saveJcIcbc(w);
                save(store);
            }
            // 判断两个地址是否相同, 不同的话, 重新入库一条商户id
//            String storeAddress = store.getStoreAddress();
//            String merAddress = dto.getMerAddress();
//            if (StringUtil.isNotEmpty(storeAddress) && StringUtil.isNotEmpty(merAddress) &&
//                    storeAddress.equals(merAddress)){
//
//            }
        }
        return store;
    }

}
