/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.base.pigFarm.service.impl;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.biz.modular.org.mapper.BizOrgMapper;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.util.UploadImage;
import vip.xiaonuo.core.param.OrgIdParam;
import vip.xiaonuo.modular.base.pigFarm.entity.PigFarm;
import vip.xiaonuo.modular.base.pigFarm.mapper.PigFarmMapper;
import vip.xiaonuo.modular.base.pigFarm.param.PigFarmIdParam;
import vip.xiaonuo.modular.base.pigFarm.service.PigFarmService;

import java.io.IOException;
import java.util.List;

/**
 * 猪场Service接口实现类
 *
 * @author newspaper
 * @date  2024/01/25 15:11
 **/
@Service
public class PigFarmServiceImpl extends ServiceImpl<PigFarmMapper, PigFarm> implements PigFarmService {

    @Autowired
    private UploadImage uploadImage;
    @Autowired
    private BizOrgMapper bizOrgMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<PigFarmIdParam> pigFarmIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(pigFarmIdParamList, PigFarmIdParam::getId));
    }

    @Override
    public PigFarm detail(PigFarmIdParam pigFarmIdParam) {
        return this.queryEntity(pigFarmIdParam.getId());
    }

    @Override
    public PigFarm queryEntity(String id) {
        PigFarm pigFarm = this.getById(id);
        if(ObjectUtil.isEmpty(pigFarm)) {
            throw new CommonException("猪场不存在，id值为：{}", id);
        }
        return pigFarm;
    }

    @Override
    public void add(String name, String size, String location, String detailLocation, String orgId, MultipartFile imgUrl) throws IOException {
        PigFarm pigFarm = new PigFarm();
        if ((this.count(new QueryWrapper<PigFarm>().lambda().eq(PigFarm::getOrgId,orgId).eq(PigFarm::getName,name))) > 0) {
            throw new CommonException("存在相同名称猪场");
        }
        pigFarm.setName(name);
        pigFarm.setLocation(location);
        pigFarm.setDetailLocation(detailLocation);
        pigFarm.setSize(size);
        pigFarm.setOrgId(orgId);
        if (ObjectUtil.isNotEmpty(imgUrl)){
            String imageCom = uploadImage.getImageCom(imgUrl);
            if ("上传失败".equals(imageCom)){
                throw new CommonException("图片上传失败");
            }
            pigFarm.setImgUrl(imageCom);
        }
        this.save(pigFarm);
    }

    @Override
    public void edit(String id, String size, String location, String detailLocation, String orgId, MultipartFile imgUrl) throws IOException {
        PigFarm pigFarm = this.queryEntity(id);
        pigFarm.setName(bizOrgMapper.selectById(orgId).getName());
        pigFarm.setLocation(location);
        pigFarm.setDetailLocation(detailLocation);
        pigFarm.setSize(size);
        pigFarm.setOrgId(orgId);
        if (ObjectUtil.isNotEmpty(imgUrl)){
            String imageCom = uploadImage.getImageCom(imgUrl);
            if ("上传失败".equals(imageCom)){
                throw new CommonException("图片上传失败");
            }
            pigFarm.setImgUrl(imageCom);
        }
        this.updateById(pigFarm);
    }

    @Override
    public PigFarm getSelf(OrgIdParam orgIdParam) {
        PigFarm one = this.getOne(new QueryWrapper<PigFarm>().lambda().eq(PigFarm::getOrgId, orgIdParam.getOrgId()));
        if (ObjectUtil.isEmpty(one)){
            one = new PigFarm();
            one.setName(bizOrgMapper.selectById(orgIdParam.getOrgId()).getName());
            one.setOrgId(orgIdParam.getOrgId());
            this.save(one);
        }
        return one;
    }
}
