package cn.iocoder.yudao.module.inspection.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.inspection.entity.DlInspectionProject;
import cn.iocoder.yudao.module.inspection.entity.ProjectRoyalty;
import cn.iocoder.yudao.module.inspection.mapper.DlInspectionProjectMapper;
import cn.iocoder.yudao.module.inspection.mapper.ProjectRoyaltyMapper;
import cn.iocoder.yudao.module.inspection.service.DlInspectionProjectService;
import cn.iocoder.yudao.module.inspection.service.ProjectRoyaltyService;
import cn.iocoder.yudao.module.inspection.vo.DlInspectionProjectPageReqVO;
import cn.iocoder.yudao.module.inspection.vo.DlInspectionProjectRespVO;
import cn.iocoder.yudao.module.inspection.vo.DlInspectionProjectSaveReqVO;
import cn.iocoder.yudao.module.inspection.vo.InspectionOrderExportVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

import static com.github.yulichang.method.SqlMethod.collect;


/**
 * 检测项目 Service 实现类
 *
 * @author 若依
 */
@Service
@Validated
public class DlInspectionProjectServiceImpl extends ServiceImpl<DlInspectionProjectMapper, DlInspectionProject> implements DlInspectionProjectService {

    @Resource
    private DlInspectionProjectMapper dlInspectionProjectMapper;

    @Resource
    private ProjectRoyaltyService projectRoyaltyService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createDlInspectionProject(DlInspectionProjectSaveReqVO createReqVO) {
        //查询项目名称是否存在
        DlInspectionProject dlInspectionProject1 = baseMapper.selectOne(new LambdaQueryWrapper<DlInspectionProject>()
                .eq(DlInspectionProject::getProjectName, createReqVO.getProjectName()));
        if (ObjectUtil.isNotNull(dlInspectionProject1)) {
            throw new RuntimeException("检测项目已存在");
        }
        // 插入
        DlInspectionProject dlInspectionProject = BeanUtils.toBean(createReqVO, DlInspectionProject.class);
        dlInspectionProject.setDeleted(false);
        dlInspectionProjectMapper.insert(dlInspectionProject);

        //插入项目子表
        List<ProjectRoyalty> projectRoyaltyList = createReqVO.getProjectRoyaltyList();
        //设置项目id
        List<ProjectRoyalty> collect = projectRoyaltyList.stream().map(item -> item.setProjectId(dlInspectionProject.getId())).collect(Collectors.toList());
        projectRoyaltyService.batchInsertProjectRoyalty(collect);
        // 返回
        return dlInspectionProject.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDlInspectionProject(DlInspectionProjectSaveReqVO updateReqVO) {

        // 更新
        DlInspectionProject updateObj = BeanUtils.toBean(updateReqVO, DlInspectionProject.class);
        dlInspectionProjectMapper.updateById(updateObj);

        //删除项目子表
        projectRoyaltyService.deleteInsertProjectRoyalty(updateReqVO.getId());
        //插入项目子表
        List<ProjectRoyalty> projectRoyaltyList = updateReqVO.getProjectRoyaltyList();
        //设置项目id
        List<ProjectRoyalty> collect = projectRoyaltyList.stream().map(item -> item.setProjectId(updateReqVO.getId())).collect(Collectors.toList());
        projectRoyaltyService.batchInsertProjectRoyalty(collect);
    }

    /**
     * 更新排序
     *
     * @param updateReqVO
     */
    @Override
    public void updateSort(DlInspectionProjectSaveReqVO updateReqVO) {
        DlInspectionProject updateObj = BeanUtils.toBean(updateReqVO, DlInspectionProject.class);
        dlInspectionProjectMapper.updateById(updateObj);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDlInspectionProject(String id) {
        // 删除
        dlInspectionProjectMapper.deleteById(id);
        //删除项目子表
        projectRoyaltyService.deleteInsertProjectRoyalty(id);
    }


    @Override
    public DlInspectionProject getDlInspectionProject(String id) {
        DlInspectionProject dlInspectionProject = dlInspectionProjectMapper.selectById(id);
        List<ProjectRoyalty> projectRoyaltyList = projectRoyaltyService.getProjectRoyaltyList(id);
        dlInspectionProject.setProjectRoyaltyList(projectRoyaltyList);
        return dlInspectionProject;
    }

    @Override
    public IPage<DlInspectionProjectRespVO> getDlInspectionProjectPage(IPage page, DlInspectionProjectPageReqVO pageReqVO) {
        return baseMapper.selectListPage(page, pageReqVO);
    }

    /**
     * 查询项目对应的检测员名称
     *
     * @param orderIds 订单id集合
     * @return
     */
    @Override
    public List<InspectionOrderExportVo> queryProjectWorkerName(List<Long> orderIds) {
        return baseMapper.queryProjectWorkerName(orderIds);
    }

}