/*
 * 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.biz.modular.depotTruckWeighDoublePic.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.depotTruckWeighDoublePic.entity.DepotTruckWeighDoublePic;
import vip.xiaonuo.biz.modular.depotTruckWeighDoublePic.mapper.DepotTruckWeighDoublePicMapper;
import vip.xiaonuo.biz.modular.depotTruckWeighDoublePic.param.DepotTruckWeighDoublePicAddParam;
import vip.xiaonuo.biz.modular.depotTruckWeighDoublePic.param.DepotTruckWeighDoublePicEditParam;
import vip.xiaonuo.biz.modular.depotTruckWeighDoublePic.param.DepotTruckWeighDoublePicIdParam;
import vip.xiaonuo.biz.modular.depotTruckWeighDoublePic.param.DepotTruckWeighDoublePicPageParam;
import vip.xiaonuo.dev.api.DevFileApi;
import vip.xiaonuo.dev.modular.file.service.DevFileService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 过磅记录图片表Service接口实现类
 *
 * @author xiang
 * @date 2023/10/10 17:32
 **/
@Service
public class DepotTruckWeighDoublePicService extends ServiceImpl<DepotTruckWeighDoublePicMapper, DepotTruckWeighDoublePic> {

    @Resource
    DevFileApi devFileApi;
    @Resource
    DevFileService devFileService;

    public Page<DepotTruckWeighDoublePic> page(DepotTruckWeighDoublePicPageParam param) {
        QueryWrapper<DepotTruckWeighDoublePic> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getDepotId())) {
            wrapper.lambda().eq(DepotTruckWeighDoublePic::getDepotId, param.getDepotId());
        }
        if (ObjectUtil.isAllNotEmpty(param.getSortField(), param.getSortOrder())) {
            CommonSortOrderEnum.validate(param.getSortOrder());
            wrapper.orderBy(true, param.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(param.getSortField()));
        } else {
            wrapper.lambda().orderByAsc(DepotTruckWeighDoublePic::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), wrapper);
    }

    public DepotTruckWeighDoublePic getOne(DepotTruckWeighDoublePic param) {
        List<DepotTruckWeighDoublePic> list = list(new LambdaQueryWrapper<>(param).orderByDesc(DepotTruckWeighDoublePic::getId));
        return list.isEmpty() ? null : list.get(0);
    }

    public void addByBase64(DepotTruckWeighDoublePicAddParam param) {
        DepotTruckWeighDoublePic doublePic = BeanUtil.toBean(param, DepotTruckWeighDoublePic.class);
        doublePic.setGrossPlateName(putPicPath(param.getGrossPlateName()));
        doublePic.setGrossLicenseName(putPicPath(param.getGrossLicenseName()));
        doublePic.setGrossCaptureHead(putPicPath(param.getGrossCaptureHead()));
        doublePic.setGrossCaptureTail(putPicPath(param.getGrossCaptureTail()));
        doublePic.setGrossCaptureWare(putPicPath(param.getGrossCaptureWare()));
        doublePic.setGrossCaptureBody(putPicPath(param.getGrossCaptureBody()));
        doublePic.setGrossCapturePoundRoom(putPicPath(param.getGrossCapturePoundRoom()));
        doublePic.setTarePlateName(putPicPath(param.getTarePlateName()));
        doublePic.setTareLicenseName(putPicPath(param.getTareLicenseName()));
        doublePic.setTareCaptureHead(putPicPath(param.getTareCaptureHead()));
        doublePic.setTareCaptureTail(putPicPath(param.getTareCaptureTail()));
        doublePic.setTareCaptureWare(putPicPath(param.getTareCaptureWare()));
        doublePic.setTareCaptureBody(putPicPath(param.getTareCaptureBody()));
        doublePic.setTareCapturePoundRoom(putPicPath(param.getTareCapturePoundRoom()));
        doublePic.setMetronome(putPicPath(param.getMetronome()));
        doublePic.setCloudGrossPlateName(putPicPath(param.getCloudGrossPlateName()));
        doublePic.setCloudGrossLicenseName(putPicPath(param.getCloudGrossLicenseName()));
        doublePic.setCloudGrossCaptureHead(putPicPath(param.getCloudGrossCaptureHead()));
        doublePic.setCloudGrossCaptureTail(putPicPath(param.getCloudGrossCaptureTail()));
        doublePic.setCloudGrossCaptureWare(putPicPath(param.getCloudGrossCaptureWare()));
        doublePic.setCloudGrossCaptureBody(putPicPath(param.getCloudGrossCaptureBody()));
        doublePic.setCloudGrossCapturePoundRoom(putPicPath(param.getCloudGrossCapturePoundRoom()));
        doublePic.setCloudTarePlateName(putPicPath(param.getCloudTarePlateName()));
        doublePic.setCloudTareLicenseName(putPicPath(param.getCloudTareLicenseName()));
        doublePic.setCloudTareCaptureHead(putPicPath(param.getCloudTareCaptureHead()));
        doublePic.setCloudTareCaptureTail(putPicPath(param.getCloudTareCaptureTail()));
        doublePic.setCloudTareCaptureWare(putPicPath(param.getCloudTareCaptureWare()));
        doublePic.setCloudTareCaptureBody(putPicPath(param.getCloudTareCaptureBody()));
        doublePic.setCloudTareCapturePoundRoom(putPicPath(param.getCloudTareCapturePoundRoom()));
        doublePic.setCloudMetronome(putPicPath(param.getCloudMetronome()));
        this.save(doublePic);
    }

    public void add(DepotTruckWeighDoublePicAddParam depotTruckWeighDoublePicAddParam) {
        DepotTruckWeighDoublePic depotTruckWeighDoublePic = BeanUtil.toBean(depotTruckWeighDoublePicAddParam, DepotTruckWeighDoublePic.class);
        this.save(depotTruckWeighDoublePic);
    }

    public void editByBookId(DepotTruckWeighDoublePicAddParam param) {
        DepotTruckWeighDoublePic one = this.getOne(new LambdaQueryWrapper<>(new DepotTruckWeighDoublePic(param.getTruckBookId(), param.getWeighId())));
        BeanUtil.copyProperties(param, one);
        this.updateById(one);
    }

    public void edit(DepotTruckWeighDoublePicEditParam depotTruckWeighDoublePicEditParam) {
        DepotTruckWeighDoublePic depotTruckWeighDoublePic = this.queryEntity(depotTruckWeighDoublePicEditParam.getId());
        BeanUtil.copyProperties(depotTruckWeighDoublePicEditParam, depotTruckWeighDoublePic);
        this.updateById(depotTruckWeighDoublePic);
    }

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

    @Transactional(rollbackFor = Exception.class)
    public void deleteByWeighIdList(List<String> weighIdList) {
        // 执行删除
        LambdaUpdateWrapper<DepotTruckWeighDoublePic> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(DepotTruckWeighDoublePic::getWeighId, weighIdList);
        this.remove(wrapper);
    }

    public DepotTruckWeighDoublePic detail(DepotTruckWeighDoublePicIdParam depotTruckWeighDoublePicIdParam) {
        return this.queryEntity(depotTruckWeighDoublePicIdParam.getId());
    }

    public DepotTruckWeighDoublePic queryEntity(String id) {
        DepotTruckWeighDoublePic depotTruckWeighDoublePic = this.getById(id);
        if (ObjectUtil.isEmpty(depotTruckWeighDoublePic)) {
            throw new CommonException("过磅记录图片表不存在，id值为：{}", id);
        }
        return depotTruckWeighDoublePic;
    }

    private String putPicPath(String base64) {
        if (Strings.isBlank(base64) || !base64.contains("base64")) return null;
        String result = null;
        /*if (!base64.contains("base64"))
            base64 = "data:image/jpg;base64," + base64;*/
        //MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), new ByteArrayInputStream(base64.getBytes()));
        return devFileApi.storageFilePathReturnUrl(base64);
    }

}
