package org.dromara.zhishu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pdd.pop.sdk.common.util.JsonUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.zhishu.domain.WarehouseSettings;
import org.dromara.zhishu.domain.dto.OrderExternalGoodsDto;
import org.dromara.zhishu.util.InterfaceUtils;
import org.dromara.zhishu.util.UrlUtil;
import org.springframework.stereotype.Service;
import org.dromara.zhishu.domain.bo.WarehouseSettingsBo;
import org.dromara.zhishu.domain.vo.WarehouseSettingsVo;
import org.dromara.zhishu.service.IWarehouseSettingsService;

import java.util.HashMap;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * 设置Service业务层处理
 *
 * @author yxy
 * @date 2025-12-19
 */
@RequiredArgsConstructor
@Service
public class WarehouseSettingsServiceImpl implements IWarehouseSettingsService {

    /**
     * 查询设置
     *
     * @param id 主键
     * @return 设置
     */
    @Override
    public WarehouseSettingsVo queryById(Long id){
        String WarehouseSettingsStr = InterfaceUtils.getInterfaceGetWithParams(UrlUtil.getOrderServiceUrl(),"/api/warehouseSetting/"+id,new HashMap<>());
        WarehouseSettingsVo WarehouseSettings = JsonUtil.transferToObj(WarehouseSettingsStr,WarehouseSettingsVo.class);
        return WarehouseSettings;
    }

    /**
     * 分页查询设置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 设置分页列表
     */
    @Override
    public TableDataInfo<WarehouseSettings> queryPageList(WarehouseSettingsBo bo, PageQuery pageQuery) {

        Map params = new HashMap<>();
        params.put("pageNum",pageQuery.getPageNum());
        params.put("pageSize",pageQuery.getPageSize());
        params.put("delFlag",0);
        String WarehouseSettingsListStr = InterfaceUtils.getInterfaceGetWithParams(UrlUtil.getOrderServiceUrl(),"/api/warehouseSetting/getList",params);
        Map dataMap = JsonUtil.transferToObj(WarehouseSettingsListStr,Map.class);
        List WarehouseSettingsList = (List) dataMap.get("data");
        int total = (int) dataMap.get("total");
        Page<WarehouseSettings> result = new Page<>(pageQuery.getPageNum(),pageQuery.getPageSize(),total);
        result.setRecords(WarehouseSettingsList);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的设置列表
     *
     * @param bo 查询条件
     * @return 设置列表
     */
    @Override
    public List<WarehouseSettingsVo> queryList(WarehouseSettingsBo bo) {
       return null;
    }



    /**
     * 新增设置
     *
     * @param bo 设置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(WarehouseSettingsBo bo) {
        bo.setCreateBy(LoginHelper.getUserId());
        bo.setUpdateBy(LoginHelper.getUserId());
        Map map = new HashMap();
        map.put("warehouseSettingsStr",JsonUtil.transferToJson(bo));
        InterfaceUtils.postForm(UrlUtil.getOrderServiceUrl(),"/api/warehouseSetting/add",map);
        return true;
    }

    /**
     * 修改设置
     *
     * @param bo 设置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(WarehouseSettingsBo bo) {
        bo.setUpdateBy(LoginHelper.getUserId());
        Map map = new HashMap();
        map.put("warehouseSettingsStr",JsonUtil.transferToJson(bo));
        InterfaceUtils.postForm(UrlUtil.getOrderServiceUrl(),"/api/warehouseSetting/edit",map);
        return true;
    }

    /**
     * 修改启用状态
     * @param bo
     * @return
     */
    @Override
    public Boolean updateStatus(WarehouseSettingsBo bo){
        bo.setUpdateBy(LoginHelper.getUserId());
        Map map = new HashMap();
        map.put("warehouseSettingsStr",JsonUtil.transferToJson(bo));
        InterfaceUtils.postForm(UrlUtil.getOrderServiceUrl(),"/api/warehouseSetting/editStatus",map);
        return true;
    }

    /**
     * 校验并批量删除设置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {

        for (Long id:ids){
            InterfaceUtils.postForm(UrlUtil.getOrderServiceUrl(),"/api/warehouseSetting/deleteById/"+id,new HashMap<>());
        }
        return true;
    }
}
