package com.yiqi.system.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiqi.common.core.domain.model.LoginUser;
import com.yiqi.common.enums.UserStatus;
import com.yiqi.common.exception.GlobalException;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.service.IGoodsSkuStoreService;
import com.yiqi.core.service.IGoodsStoreService;
import com.yiqi.system.domain.ManageFactory;
import com.yiqi.system.domain.dto.weapp.WeAppOrgQueryDTO;
import com.yiqi.system.domain.vo.OrgVO;
import com.yiqi.system.domain.vo.SysStoreVo;
import com.yiqi.system.service.IManageFactoryService;
import com.yiqi.system.service.ISysSettlementRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.system.mapper.SysStoreMapper;
import com.yiqi.system.domain.SysStore;
import com.yiqi.system.service.ISysStoreService;
import org.springframework.util.CollectionUtils;

/**
 * 门店Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-25
 */
@Service
public class SysStoreServiceImpl extends ServiceImpl<SysStoreMapper, SysStore> implements ISysStoreService {
    @Autowired
    private ISysSettlementRateService sysSettlementRateService;

    @Autowired
    private IGoodsStoreService goodsStoreService;

    @Autowired
    private IGoodsSkuStoreService goodsSkuStoreService;

    @Autowired
    private IManageFactoryService manageFactoryService;


    public List<OrgVO> allOrg(String sourceType) {
        return baseMapper.allOrg(sourceType);
    }

    @Override
    public String getStoreNameById(Long storeId) {
        SysStore sysStore = baseMapper.selectById(storeId);
        return sysStore != null ? sysStore.getName() : "";
    }

    /**
     * 查询门店
     *
     * @param id 门店主键
     * @return 门店
     */
    @Override
    public SysStore selectSysStoreById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询门店
     *
     * @return 门店
     */
    @Override
    public SysStore selectSysStoreInfoById() {
        LoginUser user = SecurityUtils.getLoginUser();
        return baseMapper.selectById(user.getStoreId());
    }

    /**
     * 查询门店列表
     *
     * @param sysStore 门店
     * @return 门店
     */
    @Override
    public List<SysStoreVo> selectSysStoreList(SysStore sysStore) {
        return baseMapper.selectSysStoreList(sysStore);
    }

    @Override
    public List<SysStoreVo> findStoreByDistance(WeAppOrgQueryDTO weAppOrgQueryDTO) {
        return baseMapper.findStoreByDistance(weAppOrgQueryDTO);
    }

    /**
     * 新增门店
     *
     * @param sysStore 门店
     * @return 结果
     */
    @Override
    public int insertSysStore(SysStore sysStore) {
        //校验id是否存在
        Map<String, Object> map = new HashMap<>();
        map.put("code", sysStore.getCode());
        map.put("del_flag", UserStatus.OK.getCode());
        if (!CollectionUtils.isEmpty(baseMapper.selectByMap(map))) {
            throw new ServiceException("已存在该门店编号");
        }
        ManageFactory manageFactory = manageFactoryService.getById(sysStore.getFactoryId());
        if (manageFactory == null) {
            throw new GlobalException("不存在该工厂");
        }
        sysStore.setBindAreaId(manageFactory.getBindAreaId());
        sysStore.buildCreateData();
        sysStore.setStatus(UserStatus.OK.getCode());
        sysStore.setDelFlag(UserStatus.OK.getCode());
        int i = baseMapper.insert(sysStore);
        // 新增门店和全部商品的关系
        goodsStoreService.insertStoreToAllGoods(sysStore);
        // 新增门店和全部商品物料的关系
        goodsSkuStoreService.insertStoreToAllSku(sysStore);
        // 新增结算费率关系
//        sysSettlementRateService.insertStoreRate(sysStore);
        return i;
    }

    /**
     * 修改门店
     *
     * @param sysStore 门店
     * @return 结果
     */
    @Override
    public int updateSysStore(SysStore sysStore) {
        QueryWrapper<SysStore> storeQueryWrapper = new QueryWrapper<SysStore>()
                .eq("code", sysStore.getCode())
                .eq("del_flag", UserStatus.OK.getCode())
                .ne("id", sysStore.getId());
        if (!CollectionUtils.isEmpty(baseMapper.selectList(storeQueryWrapper))) {
            throw new ServiceException("已存在该门店编号");
        }
        sysStore.buildUpdateData();
        return baseMapper.updateById(sysStore);
    }

    /**
     * 批量删除门店
     *
     * @param ids 需要删除的门店主键
     * @return 结果
     */
    @Override
    public int deleteSysStoreByIds(Long[] ids) {
        return baseMapper.deleteSysStoreByIds(ids);
    }

    /**
     * 删除门店信息
     *
     * @param id 门店主键
     * @return 结果
     */
    @Override
    public int deleteSysStoreById(Long id) {
        return baseMapper.deleteSysStoreById(id);
    }

    @Override
    public int updateStoreStatus(SysStore sysStore) {
        sysStore.setUpdateTime(DateUtils.getNowDate());
        sysStore.setUpdateBy(SecurityUtils.getUsername());
        sysStore.setUpdateById(SecurityUtils.getUserId());
        return baseMapper.updateById(sysStore);
    }
}
