package com.yuchen.saas.park.service.service.fire.fight.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.fire.fight.FireFightDutyStaffReqDTO;
import com.yuchen.saas.park.api.dto.fire.fight.FireFightDutyStaffSubmitReqDTO;
import com.yuchen.saas.park.api.entity.fire.fight.SaasParkFireFightDutyStaff;
import com.yuchen.saas.park.api.vo.fire.fight.FireFightDutyStaffVO;
import com.yuchen.saas.park.service.mapper.fire.fight.SaasParkFireFightDutyStaffMapper;
import com.yuchen.saas.park.service.service.fire.fight.ISaasParkFireFightDutyStaffService;
import com.yuchen.saas.park.service.util.PageConverterUtils;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.mp.base.BaseEntity;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.secure.ContextUser;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 消防值班人员信息 服务实现类
 * </p>
 *
 * @author zhouhanwei
 * @since 2024-04-30
 */
@Service
public class SaasParkFireFightDutyStaffServiceImpl extends BaseServiceImpl<SaasParkFireFightDutyStaffMapper, SaasParkFireFightDutyStaff> implements ISaasParkFireFightDutyStaffService {

    @Override
    public NestPage<FireFightDutyStaffVO> findFireInspectTypePage(FireFightDutyStaffReqDTO reqDTO) {
        ContextUser contextUser = AuthUtil.getUser();
        if (Objects.isNull(contextUser)) {
            throw new ServiceException("用户不存在");
        }
        Page<FireFightDutyStaffVO> resultPage = new Page<>();

        // 赋值数据
        LambdaQueryWrapper<SaasParkFireFightDutyStaff> lambdaWrapper = Wrappers.lambdaQuery();
        lambdaWrapper.eq(SaasParkFireFightDutyStaff::getCustomerId, contextUser.getCustomerManageId());
        lambdaWrapper.eq(Func.isNotEmpty(reqDTO.getProjectId()), SaasParkFireFightDutyStaff::getProjectId, reqDTO.getProjectId());
        lambdaWrapper.like(Func.isNotEmpty(reqDTO.getOrgUserName()), SaasParkFireFightDutyStaff::getOrgUserName, reqDTO.getOrgUserName());
        lambdaWrapper.like(Func.isNotEmpty(reqDTO.getDeptName()), SaasParkFireFightDutyStaff::getDeptName, reqDTO.getDeptName());
        lambdaWrapper.like(Func.isNotEmpty(reqDTO.getJobNo()), SaasParkFireFightDutyStaff::getJobNo, reqDTO.getJobNo());
        lambdaWrapper.like(Func.isNotEmpty(reqDTO.getPosition()), SaasParkFireFightDutyStaff::getPosition, reqDTO.getPosition());
        lambdaWrapper.like(Func.isNotEmpty(reqDTO.getPhone()), SaasParkFireFightDutyStaff::getPhone, reqDTO.getPhone());
        lambdaWrapper.eq(Func.isNotEmpty(reqDTO.getStatus()), SaasParkFireFightDutyStaff::getStatus, reqDTO.getStatus());
        lambdaWrapper.orderByDesc(BaseEntity::getCreateTime);
        IPage<SaasParkFireFightDutyStaff> page = page(Condition.getPage(reqDTO), lambdaWrapper);
        List<SaasParkFireFightDutyStaff> fireInspectTypeList = page.getRecords();

        // 数据列表
        List<FireFightDutyStaffVO> resultList = new ArrayList<>();

        for (SaasParkFireFightDutyStaff saasParkFireFightDutyStaff : fireInspectTypeList) {
            FireFightDutyStaffVO vo = new FireFightDutyStaffVO();
            BeanUtil.copyProperties(saasParkFireFightDutyStaff, vo);
            resultList.add(vo);
        }
        resultPage.setTotal(page.getTotal());
        resultPage.setSize(page.getSize());
        resultPage.setCurrent(page.getCurrent());
        resultPage.setRecords(resultList);
        return PageConverterUtils.convert(resultPage);
    }

    @Override
    public FireFightDutyStaffVO getFireFightDutyStaffById(Long staffId) {
        FireFightDutyStaffVO vo = new FireFightDutyStaffVO();
        SaasParkFireFightDutyStaff saasParkFireFightDutyStaff = getById(staffId);
        if (Objects.isNull(saasParkFireFightDutyStaff)) {
            throw new ServiceException("值班人员数据不存在");
        }
        BeanUtil.copyProperties(saasParkFireFightDutyStaff, vo);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addFireFightDutyStaff(FireFightDutyStaffSubmitReqDTO reqDTO) {
        ContextUser contextUser = AuthUtil.getUser();
        if (Objects.isNull(contextUser)) {
            throw new ServiceException("用户不存在");
        }
        SaasParkFireFightDutyStaff saasParkFireFightDutyStaff = new SaasParkFireFightDutyStaff();
        BeanUtil.copyProperties(reqDTO, saasParkFireFightDutyStaff);
        // 判断去重
        validRepeat(saasParkFireFightDutyStaff);
        saasParkFireFightDutyStaff.setCustomerId(AuthUtil.getUser().getCustomerManageId());
        saasParkFireFightDutyStaff.setCreateOrgUser(AuthUtil.getUser().getOrgUserId());
        return save(saasParkFireFightDutyStaff);
    }

    @Override
    public Boolean editFireFightDutyStaff(FireFightDutyStaffSubmitReqDTO reqDTO) {
        ContextUser contextUser = AuthUtil.getUser();
        if (Objects.isNull(contextUser)) {
            throw new ServiceException("用户不存在");
        }
        SaasParkFireFightDutyStaff saasParkFireFightDutyStaff = getById(reqDTO.getId());
        if (Objects.isNull(saasParkFireFightDutyStaff)) {
            throw new ServiceException("值班人员数据不存在");
        }
        BeanUtil.copyProperties(reqDTO, saasParkFireFightDutyStaff);
        // 判断去重
        validRepeat(saasParkFireFightDutyStaff);
        saasParkFireFightDutyStaff.setUpdateOrgUser(AuthUtil.getUser().getOrgUserId());
        return updateById(saasParkFireFightDutyStaff);
    }

    @Override
    public Boolean removeFireFightDutyStaff(Long staffId) {
        SaasParkFireFightDutyStaff saasParkFireFightDutyStaff = getById(staffId);
        if (Objects.isNull(saasParkFireFightDutyStaff)) {
            throw new ServiceException("值班数据不存在");
        }
        return removeById(staffId);
    }

    private void validRepeat(SaasParkFireFightDutyStaff saasParkFireFightDutyStaff) {
        int count = count(new LambdaQueryWrapper<SaasParkFireFightDutyStaff>()
                .eq(SaasParkFireFightDutyStaff::getOrgUserId, saasParkFireFightDutyStaff.getOrgUserId())
                .eq(SaasParkFireFightDutyStaff::getProjectId, saasParkFireFightDutyStaff.getProjectId())
                .notIn(Objects.nonNull(saasParkFireFightDutyStaff.getId()), BaseEntity::getId, saasParkFireFightDutyStaff.getId()));
        if (count > 0) {
            throw new ServiceException("值班人员选择不能重复");
        }
    }
}
