package com.ruoyi.postCheck.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.postCheck.domain.PostCheckedItems;
import com.ruoyi.postCheck.service.IPostCheckedItemsService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.postCheck.mapper.PostCheckedMapper;
import com.ruoyi.postCheck.domain.PostChecked;
import com.ruoyi.postCheck.service.IPostCheckedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 岗检表管理Service业务层处理
 *
 * @author csg
 * @date 2024-01-05
 */
@Service
@Slf4j
public class PostCheckedServiceImpl extends ServiceImpl<PostCheckedMapper, PostChecked> implements IPostCheckedService {
    @Autowired
    private PostCheckedMapper postCheckedMapper;
    @Autowired
    private IPostCheckedItemsService postCheckedItemsService;

    /**
     * 查询岗检表管理
     *
     * @param id 岗检表管理主键
     * @return 岗检表管理
     */
    @Override
    public PostChecked selectPostCheckedById(Long id) {
        PostChecked data = postCheckedMapper.selectPostCheckedById(id);
        if (data != null) {
//            Long id = data.getId();
//            if (id != null) {

            PostCheckedItems param = new PostCheckedItems();
            param.setPostCheckedId(id);
            List<PostCheckedItems> children = postCheckedItemsService.selectPostCheckedItemsList(param);
            data.setChildren(children);
//            }
        }
        return data;
    }

    /**
     * 查询岗检表管理列表
     *
     * @param postChecked 岗检表管理
     * @return 岗检表管理
     */
    @Override
    public List<PostChecked> selectPostCheckedList(PostChecked postChecked) {
        List<PostChecked> dataList = postCheckedMapper.selectPostCheckedList(postChecked);
        if (!CollectionUtils.isEmpty(dataList)) {
            for (PostChecked data : dataList) {
                Long id = data.getId();
                if (id != null) {

                    PostCheckedItems param = new PostCheckedItems();
                    param.setPostCheckedId(id);
                    List<PostCheckedItems> children = postCheckedItemsService.selectPostCheckedItemsList(param);
                    data.setChildren(children);
                }
            }
        }
        return dataList;
    }

    /**
     * 新增岗检表管理
     *
     * @param postChecked 岗检表管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPostChecked(PostChecked postChecked) {

        List<PostCheckedItems> children = postChecked.getChildren();
        if (CollectionUtils.isEmpty(children)) {
            log.error("【岗检表】-add：未设置检查内容，请在页面选择需要检查的项目，再提交！");
            throw new BaseException("未设置检查内容，请在页面选择需要检查的项目，再提交！");
        }
        //TODO 自增主键修改为-MyBatis-Plus雪花算法(截取5位解决VUE精度丢失BUG)-系统登录日志
//        if (postChecked.getId() == null)
        postChecked.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
        // postChecked.setCreateTime(DateUtils.getNowDate());
        postChecked.setCreateDefault();
//        postChecked.setStatus(PostCheckedEnum.STATUS_WAIT.getValue()); // 去创建自己的枚举类
        postChecked.setCreateUserId(SecurityUtils.getUserId());
        int row = postCheckedMapper.insertPostChecked(postChecked);
        if (row > 0) {
            for (PostCheckedItems child : children) {
                child.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
                child.setPostCheckedId(postChecked.getId());
                child.setCreateDefault();
            }
            postCheckedItemsService.saveBatch(children);
        }
        return row;
    }

    /**
     * 修改岗检表管理
     *
     * @param postChecked 岗检表管理
     * @return 结果
     */
    @Override
    public int updatePostChecked(PostChecked postChecked) {
        Long id = postChecked.getId();

        if (id == null) {
            log.error("【岗检表】-edit：未设置id，请联系管理员！");
            throw new BaseException("未设置id，请联系管理员！");
        }

        List<PostCheckedItems> children = postChecked.getChildren();
        if (CollectionUtils.isEmpty(children) || id == null) {
            log.error("【岗检表】-edit：未设置检查内容，请在页面选择需要检查的项目，再提交！");
            throw new BaseException("未设置检查内容，请在页面选择需要检查的项目，再提交！");
        }


        postChecked.setUpdateDefault();
        int row = postCheckedMapper.updatePostChecked(postChecked);
        if (row > 0) {
            //删除明细
            LambdaUpdateWrapper<PostCheckedItems> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(PostCheckedItems::getPostCheckedId, id);
            postCheckedItemsService.remove(updateWrapper);

            //插入明细
            for (PostCheckedItems child : children) {
                child.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
                child.setPostCheckedId(postChecked.getId());
                child.setCreateDefault();
            }
            postCheckedItemsService.saveBatch(children);
        }

        return row;
    }

    /**
     * 批量删除岗检表管理
     *
     * @param ids 需要删除的岗检表管理主键
     * @return 结果
     */
    @Override
    public int deletePostCheckedByIds(Long[] ids) {
        int row = postCheckedMapper.deletePostCheckedByIds(ids);

        //删除明细
        LambdaUpdateWrapper<PostCheckedItems> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(PostCheckedItems::getPostCheckedId, ids);
        postCheckedItemsService.remove(updateWrapper);

        return row;
    }

    /**
     * 删除岗检表管理信息
     *
     * @param id 岗检表管理主键
     * @return 结果
     */
    @Override
    public int deletePostCheckedById(Long id) {
        int row = postCheckedMapper.deletePostCheckedById(id);
        //删除明细
        LambdaUpdateWrapper<PostCheckedItems> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(PostCheckedItems::getPostCheckedId, id);
        postCheckedItemsService.remove(updateWrapper);
        return row;
    }
}
