package com.yiqi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiqi.common.enums.UserStatus;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.common.utils.StringUtils;
import com.yiqi.core.service.IGoodsSkuStoreService;
import com.yiqi.core.service.IGoodsStoreService;
import com.yiqi.system.domain.ManageFactory;
import com.yiqi.system.domain.SysStore;
import com.yiqi.system.mapper.ManageFactoryMapper;
import com.yiqi.system.mapper.SysStoreMapper;
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 org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 工厂 基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-20
 */
@Service
public class ManageFactoryServiceImpl extends ServiceImpl<ManageFactoryMapper, ManageFactory> implements
        IManageFactoryService {

    @Autowired
    private ISysSettlementRateService sysSettlementRateService;
    @Autowired
    private SysStoreMapper storeMapper;

    @Autowired
    private IGoodsStoreService goodsStoreService;

    @Autowired
    private IGoodsSkuStoreService goodsSkuStoreService;

    /**
     * 查询工厂 基本信息
     *
     * @param id 工厂 基本信息主键
     * @return 工厂 基本信息
     */
    @Override
    public ManageFactory selectYiqiManageFactoryById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询工厂 基本信息列表
     *
     * @param manageFactory 工厂 基本信息
     * @return 工厂 基本信息
     */
    @Override
    public List<ManageFactory> selectYiqiManageFactoryList(ManageFactory manageFactory) {
        QueryWrapper<ManageFactory> factoryWrapper = new QueryWrapper<ManageFactory>()
                .eq(manageFactory.getProvinceId() != null, "province_id", manageFactory.getProvinceId())
                .eq(manageFactory.getCityId() != null, "city_id", manageFactory.getCityId())
                .eq(manageFactory.getAreaId() != null, "area_id", manageFactory.getAreaId())
                .eq("del_flag", UserStatus.OK.getCode())
                .like(StringUtils.isNotBlank(manageFactory.getFactoryName()), "factory_name", manageFactory.getFactoryName())
                .orderByDesc("id");
        return baseMapper.selectList(factoryWrapper);
    }

    /**
     * 新增工厂 基本信息
     *
     * @param manageFactory 工厂 基本信息
     * @return 结果
     */
    @Override
    public int insertYiqiManageFactory(ManageFactory manageFactory) {
        manageFactory.setCreateTime(DateUtils.getNowDate());
        manageFactory.setCreateBy(SecurityUtils.getUsername());
        manageFactory.setCreateById(SecurityUtils.getUserId());
        manageFactory.setDelFlag(UserStatus.OK.getCode());
        manageFactory.setStatus(UserStatus.OK.getCode());
        int i = baseMapper.insert(manageFactory);
        // 新增工厂和全部商品的关系
//        goodsStoreService.insertFactoryToAllGoods(manageFactory);
        // 新增工厂和全部商品物料的关系
//        goodsSkuStoreService.insertFactoryToAllSku(manageFactory);
        // 新增结算费率关系
//        sysSettlementRateService.insertFactoryRate(manageFactory);
        return i;
    }

    /**
     * 修改工厂 基本信息
     *
     * @param manageFactory 工厂 基本信息
     * @return 结果
     */
    @Override
    public int updateYiqiManageFactory(ManageFactory manageFactory) {
        manageFactory.setUpdateTime(DateUtils.getNowDate());
        manageFactory.setUpdateBy(SecurityUtils.getUsername());
        manageFactory.setUpdateById(SecurityUtils.getUserId());
        return baseMapper.updateById(manageFactory);
    }

    /**
     * 批量删除工厂 基本信息
     *
     * @param ids 需要删除的工厂 基本信息主键
     * @return 结果
     */
    @Override
    public int deleteYiqiManageFactoryByIds(Long[] ids) {
        //验证工厂是否已有门店绑定
        SysStore store = new SysStore();
        store.setFactoryId(ids[0]);
        if (!CollectionUtils.isEmpty(storeMapper.selectSysStoreList(store))) {
            throw new ServiceException("该工厂已绑定门店,删除失败");
        }
        return baseMapper.deleteYiqiManageFactoryByIds(ids);
    }

    /**
     * 删除工厂 基本信息信息
     *
     * @param id 工厂 基本信息主键
     * @return 结果
     */
    @Override
    public int deleteYiqiManageFactoryById(Long id) {
        //验证工厂是否已有门店绑定
        SysStore store = new SysStore();
        store.setFactoryId(id);
        if (!CollectionUtils.isEmpty(storeMapper.selectSysStoreList(store))) {
            throw new ServiceException("该工厂已绑定门店,删除失败");
        }
        return baseMapper.deleteYiqiManageFactoryById(id);
    }

    @Override
    public int updateFactoryStatus(ManageFactory manageFactory) {
        manageFactory.setUpdateTime(DateUtils.getNowDate());
        manageFactory.setUpdateBy(SecurityUtils.getUsername());
        manageFactory.setUpdateById(SecurityUtils.getUserId());
        return baseMapper.updateById(manageFactory);
    }
}
