package com.ruoyi.zhdd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.zhdd.domain.ResourceDetail;
import com.ruoyi.zhdd.domain.bo.ResourceDetailBo;
import com.ruoyi.zhdd.service.IResourceDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhdd.domain.bo.ResourceBo;
import com.ruoyi.zhdd.domain.vo.ResourceVo;
import com.ruoyi.zhdd.domain.Resource;
import com.ruoyi.zhdd.mapper.ResourceMapper;
import com.ruoyi.zhdd.service.IResourceService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 应急资源Service业务层处理
 *
 * @author xintong
 * @date 2021-10-27
 */
@Service
public class ResourceServiceImpl extends ServicePlusImpl<ResourceMapper, Resource, ResourceVo> implements IResourceService {

    @Autowired
    private IResourceDetailService resourceDetailService;

    @Override
    public ResourceVo queryById(String id){
        ResourceVo voById = getVoById(id);
        if (voById.getResourceType() == 1) {
            // 应急仓库的类型查询仓库物资
            List<ResourceDetail> list = resourceDetailService.list(Wrappers.<ResourceDetail>lambdaQuery().eq(ResourceDetail::getResourceId, id));
            voById.setResourceDetailList(list);
        }
        return voById;
    }

    @Override
    public TableDataInfo<ResourceVo> queryPageList(ResourceBo bo) {
        PagePlus<Resource, ResourceVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ResourceVo> queryList(ResourceBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<Resource> buildQueryWrapper(ResourceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Resource> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getResourceType() != null, Resource::getResourceType, bo.getResourceType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Resource::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), Resource::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), Resource::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), Resource::getLatitude, bo.getLatitude());
        lqw.like(StringUtils.isNotBlank(bo.getManageUnit()), Resource::getManageUnit, bo.getManageUnit());
        lqw.eq(bo.getNum() != null, Resource::getNum, bo.getNum());
        lqw.eq(StringUtils.isNotBlank(bo.getCarryGoods()), Resource::getCarryGoods, bo.getCarryGoods());
        lqw.like(StringUtils.isNotBlank(bo.getContactName()), Resource::getContactName, bo.getContactName());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhone()), Resource::getContactPhone, bo.getContactPhone());
        return lqw;
    }

    @Override
    public Boolean insertByBo(ResourceBo bo) {
        Resource add = BeanUtil.toBean(bo, Resource.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(ResourceBo bo) {
        Resource update = BeanUtil.toBean(bo, Resource.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Resource entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        // 删除资源明细内容
        resourceDetailService.remove(Wrappers.<ResourceDetail>lambdaQuery().in(ResourceDetail::getResourceId, ids));
        return removeByIds(ids);
    }

    /**
     * 插入应急资源数据
     * @param bo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertResource(ResourceBo bo) {
        if (bo.getResourceType() == 3) {
            // 车牌号转大写
            bo.setName(bo.getName().toUpperCase());
        }
        Resource add = BeanUtil.toBean(bo, Resource.class);
        boolean save = save(add);
        if (save) {
            if (bo.getResourceDetailList() != null && bo.getResourceDetailList().size() > 0) {
                // 处理应急物资内容
                List<ResourceDetail> list = new ArrayList<>();
                for (ResourceDetailBo resourceDetailBo : bo.getResourceDetailList()) {
                    resourceDetailBo.setResourceId(add.getId());
                    ResourceDetail resourceDetail = BeanUtil.toBean(resourceDetailBo, ResourceDetail.class);
                    list.add(resourceDetail);
                }
                resourceDetailService.saveAll(list);
            }
        }
        return save;
    }

    /**
     * 更新资源信息
     * @param bo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateResource(ResourceBo bo) {
        if (bo.getResourceType() == 3) {
            // 车牌号转大写
            bo.setName(bo.getName().toUpperCase());
        }
        Resource update = BeanUtil.toBean(bo, Resource.class);
        boolean save = updateById(update);
        if (save) {
            if (bo.getResourceDetailList() != null && bo.getResourceDetailList().size() > 0) {
                // 处理应急物资内容
                // 先删除原有的数据
                resourceDetailService.remove(Wrappers.<ResourceDetail>lambdaQuery().eq(ResourceDetail::getResourceId, update.getId()));
                List<ResourceDetail> list = new ArrayList<>();
                for (ResourceDetailBo resourceDetailBo : bo.getResourceDetailList()) {
                    resourceDetailBo.setResourceId(update.getId());
                    resourceDetailBo.setId(null);
                    ResourceDetail resourceDetail = BeanUtil.toBean(resourceDetailBo, ResourceDetail.class);
                    list.add(resourceDetail);
                }
                resourceDetailService.saveAll(list);
            }
        }
        return save;
    }

}
