package com.njzscloud.supervisory.order.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
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.config.AppProperties;
import com.njzscloud.supervisory.order.mapper.OrderCargoPlaceMapper;
import com.njzscloud.supervisory.order.pojo.entity.OrderCargoPlaceEntity;
import com.njzscloud.supervisory.order.pojo.param.AddOrderCargoPlaceParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 装货地址
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderCargoPlaceService extends ServiceImpl<OrderCargoPlaceMapper, OrderCargoPlaceEntity> implements IService<OrderCargoPlaceEntity> {
    private final AppProperties appProperties;

    /**
     * 新增
     *
     * @return
     */
    public long add(AddOrderCargoPlaceParam addOrderCargoPlaceParam) {
        OrderCargoPlaceEntity orderCargoPlaceEntity = BeanUtil.copyProperties(addOrderCargoPlaceParam, OrderCargoPlaceEntity.class);
        AppProperties.DefaultPlace defaultPlace = appProperties.getDefaultPlace();
        this.save(orderCargoPlaceEntity
                .setProvince(defaultPlace.getProvince())
                .setCity(defaultPlace.getCity())
                .setProvinceName(defaultPlace.getProvinceName())
                .setCityName(defaultPlace.getCityName())
        );
        return orderCargoPlaceEntity.getId();
    }

    /**
     * 修改
     */
    public void modify(OrderCargoPlaceEntity orderCargoPlaceEntity) {
        this.updateById(orderCargoPlaceEntity);
    }

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

    /**
     * 详情
     */
    public OrderCargoPlaceEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<OrderCargoPlaceEntity> paging(PageParam pageParam, OrderCargoPlaceEntity orderCargoPlaceEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<OrderCargoPlaceEntity>query(orderCargoPlaceEntity)));
    }

}
