package com.njzscloud.supervisory.goods.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.biz.pojo.entity.BizCompanyEntity;
import com.njzscloud.supervisory.biz.service.BizCompanyService;
import com.njzscloud.supervisory.goods.mapper.GoodsCategoryMapper;
import com.njzscloud.supervisory.goods.pojo.entity.GoodsCategoryEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 产品类型
 */
@Slf4j
@Service
public class GoodsCategoryService extends ServiceImpl<GoodsCategoryMapper, GoodsCategoryEntity> {

    @Autowired
    private BizCompanyService bizCompanyService;

    /**
     * 新增
     */
    public void add(GoodsCategoryEntity goodsCategoryEntity) {
        goodsCategoryEntity.setEnable(true);
        this.save(goodsCategoryEntity);
    }

    /**
     * 修改
     */
    public void modify(GoodsCategoryEntity goodsCategoryEntity) {
        this.updateById(goodsCategoryEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public GoodsCategoryEntity detail(Long id) {
        GoodsCategoryEntity entity = this.getById(id);
        if (null != entity) {
            BizCompanyEntity companyEntity = bizCompanyService.getById(entity.getStationId());
            if (null != companyEntity) {
                entity.setStationName(companyEntity.getCompanyName());
            }
        }
        return entity;
    }

    /**
     * 分页查询
     */
    public PageResult<GoodsCategoryEntity> paging(PageParam pageParam, GoodsCategoryEntity goodsCategoryEntity) {
        return PageResult.of(this.baseMapper.pagingWithStationName(pageParam.toPage(), goodsCategoryEntity));
    }

    /**
     * 启用/禁用
     */
    public void enable(Long id, Boolean enable) {
        GoodsCategoryEntity entity = new GoodsCategoryEntity();
        entity.setId(id);
        entity.setEnable(enable);
        this.updateById(entity);
    }

}
