package com.jwipc.tender.logic.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;

import com.jwipc.tender.convert.ExpertManageConvert;
import com.jwipc.tender.dto.ExpertManageDTO;
import com.jwipc.tender.dto.ExpertManageSaveDTO;
import com.jwipc.tender.entity.ExpertManage;
import com.jwipc.tender.entity.PersonDimension;
import com.jwipc.tender.logic.ExpertManageLogic;

import com.jwipc.tender.model.po.ExpertManagePO;
import com.jwipc.tender.model.query.ExpertManageQuery;
import com.jwipc.tender.service.IExpertManageService;
import com.jwipc.tender.service.IPersonDimensionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author AL
 * @date 2024/1/31 15:01
 * @desc 模具
 */
@Service
@RequiredArgsConstructor
public class ExpertManageLogicImpl implements ExpertManageLogic {

    private final IExpertManageService expertManageService;
    private final ExpertManageConvert expertManageConvert;
    private final IPersonDimensionService personDimensionService;

    @Override
    public PageView findByPage(ExpertManageQuery query) {
        final Page<ExpertManage> dbPage = PageUtil.toPage(query);

        List<ExpertManagePO> expertManagePOS = expertManageService.findPageByQuery(dbPage, query);

        return PageUtil.getPV(expertManageConvert.po2PageListDTO(expertManagePOS), dbPage.getTotal());
    }
    @Override
    public ExpertManageDTO getById(Long id) {
        if (id != null) {
            ExpertManagePO ExpertManagePO = expertManageService.getDataById(id);
            return expertManageConvert.po2DTO(ExpertManagePO);
        }else{
            return new ExpertManageDTO();
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(ExpertManageSaveDTO param) {
        //保存
        ExpertManage expertManage = expertManageConvert.dto2Entity(param);
        boolean isOk = false;
        Long personId ;
        if (Objects.isNull(param.getId())){
            expertManageService.save(expertManage);
            personId=expertManage.getId();
            final List<String> dimensionCodes = param.getDimensionCodes();
            List<PersonDimension> insertLists = Lists.newArrayList();
            for (String dimensionCode : dimensionCodes) {
                PersonDimension tmp = new PersonDimension();
                tmp.setDimensionCode(dimensionCode);
                tmp.setPersonId(personId);
                insertLists.add(tmp);
            }
            if (CollectionUtil.isNotEmpty(insertLists)) {
                isOk = personDimensionService.saveBatch(insertLists);
                if (!isOk) {
                    throw new DaoException("插入评分人维度关系数据失败!");
                }
            }
        }else {
            personDimensionService.remove(new LambdaQueryWrapper<PersonDimension>().eq(
                    PersonDimension::getPersonId, param.getId()));
            //添加新的关系
            personId=expertManage.getId();
            final List<String> dimensionCodes = param.getDimensionCodes();
            List<PersonDimension> insertLists = Lists.newArrayList();
            for (String dimensionCode : dimensionCodes) {
                PersonDimension tmp = new PersonDimension();
                tmp.setDimensionCode(dimensionCode);
                tmp.setPersonId(personId);
                insertLists.add(tmp);
            }
            expertManageService.updateById(expertManage);
        }
    }
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void save(ExpertManageSaveDTO evlPersonDto) {
//        ExpertManage expertManage =
//                BeanUtil.mapToBean(BeanUtil.beanToMap(evlPersonDto), ExpertManage.class, true);
//        boolean isOk = false;
//        Long personId = expertManage.getId();
//        if (Objects.isNull(personId) || personId.compareTo(0L) == 0) {
//            final ExpertManage byUserId = this.findByUserId(expertManage.getUserId());
//            if (!Objects.isNull(byUserId)) {
//                expertManage.setUserId(byUserId.getUserId());
//                expertManage.setId(byUserId.getId());
//                isOk = updateAllColumnById(expertManage);
//                if (!isOk) {
//                    throw new DaoException("更新评分人数据失败!");
//                }
//            } else {
//                isOk = insert(expertManage);
//                if (!isOk) {
//                    throw new DaoException("插入评分人数据失败!");
//                }
//            }
//            personId = expertManage.getId();
//        } else {
//            isOk = updateById(expertManage);
//            if (!isOk) {
//                throw new DaoException("更新评分人数据失败!");
//            }
//        }
//
//        final List<String> dimensionCodes = evlPersonDto.getDimensionCodes();
//        isOk = expertManageService.deleteByPersonId(personId);
//        if (!isOk) {
//            throw new DaoException("删除评分人维度关系数据失败!");
//        }
//        List<PersonDimension> insertLists = Lists.newArrayList();
//        for (String dimensionCode : dimensionCodes) {
//            PersonDimension tmp = new PersonDimension();
//            tmp.setDimensionCode(dimensionCode);
//            tmp.setPersonId(personId);
//            insertLists.add(tmp);
//        }
//        if (CollectionUtil.isNotEmpty(insertLists)) {
//            isOk = personDimensionService.insertBatch(insertLists);
//            if (!isOk) {
//                throw new DaoException("插入评分人维度关系数据失败!");
//            }
//        }
//
//        isOk = tenderEvlPersonCategoryRefService.deleteByPersonId(personId);
//        if (!isOk) {
//            throw new DaoException("删除评分人采购类别关系数据失败!");
//        }
//        final List<Long> categoryId = evlPersonDto.getCategoryId();
//        List<TenderEvlPersonCategoryRef> insertCategorys = Lists.newArrayList();
//        for (Long aLong : categoryId) {
//            TenderEvlPersonCategoryRef tmp = new TenderEvlPersonCategoryRef();
//            tmp.setPersonId(personId);
//            tmp.setCategoryId(aLong);
//            insertCategorys.add(tmp);
//        }
//        if (CollectionUtil.isNotEmpty(insertCategorys)) {
//            isOk = tenderEvlPersonCategoryRefService.insertBatch(insertCategorys, 300);
//            if (!isOk) {
//                throw new DaoException("插入评分人采购类别数据失败!");
//            }
//        }
//    }
    @Override
    public void remove(String id) {
        //删除
        expertManageService.remove(new LambdaQueryWrapper<ExpertManage>().eq(ExpertManage::getId, id));
    }
//    @Override
//    public List<ExpertManageDTO> listAll() {
//        return expertManageConvert.entity2ListDTO(expertManageService.list());
//    }
}
