package shop.alien.lawyer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerExpertiseArea;
import shop.alien.lawyer.service.LawyerExpertiseAreaService;
import shop.alien.mapper.LawyerExpertiseAreaMapper;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.Date;
import java.util.List;

/**
 * 领域信息 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class LawyerExpertiseAreaServiceImpl extends ServiceImpl<LawyerExpertiseAreaMapper, LawyerExpertiseArea> implements LawyerExpertiseAreaService {

    private final LawyerExpertiseAreaMapper expertiseAreaMapper;

    @Override
    public R<IPage<LawyerExpertiseArea>> getExpertiseAreaList(int pageNum, int pageSize, String expertiseAreaInfo) {
        log.info("LawyerExpertiseAreaServiceImpl.getExpertiseAreaList?pageNum={},pageSize={},expertiseAreaInfo={}",
                pageNum, pageSize, expertiseAreaInfo);
        Page<LawyerExpertiseArea> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LawyerExpertiseArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerExpertiseArea::getDeleteFlag, 0);
        if (StringUtils.isNotBlank(expertiseAreaInfo)) {
            queryWrapper.like(LawyerExpertiseArea::getExpertiseAreaInfo, expertiseAreaInfo);
        }
        queryWrapper.orderByDesc(LawyerExpertiseArea::getCreatedTime);
        IPage<LawyerExpertiseArea> pageResult = this.page(page, queryWrapper);
        return R.data(pageResult);
    }

    @Override
    public R<LawyerExpertiseArea> addExpertiseArea(LawyerExpertiseArea expertiseArea) {
        log.info("LawyerExpertiseAreaServiceImpl.addExpertiseArea?expertiseArea={}", expertiseArea);

        // 参数校验
        if (expertiseArea == null) {
            log.warn("新增领域信息失败：参数为空");
            return R.fail("参数不能为空");
        }
        // 参数校验
        if (StringUtils.isBlank(expertiseArea.getExpertiseAreaInfo())) {
            log.warn("新增领域信息失败：领域名称为空");
            return R.fail("领域名称不能为空");
        }

        // 设置默认值
        if (expertiseArea.getDeleteFlag() == null) {
            expertiseArea.setDeleteFlag(0);
        }
        if (expertiseArea.getCreatedTime() == null) {
            expertiseArea.setCreatedTime(new Date());
        }
        if (expertiseArea.getUpdatedTime() == null) {
            expertiseArea.setUpdatedTime(new Date());
        }

        boolean result = this.save(expertiseArea);
        if (result) {
            log.info("新增领域信息成功，ID={}", expertiseArea.getId());
            return R.data(expertiseArea);
        }
        return R.fail("新增失败");
    }

    @Override
    public R<LawyerExpertiseArea> editExpertiseArea(LawyerExpertiseArea expertiseArea) {
        log.info("LawyerExpertiseAreaServiceImpl.editExpertiseArea?expertiseArea={}", expertiseArea);

        // 参数校验
        if (expertiseArea == null || expertiseArea.getId() == null) {
            log.warn("编辑领域信息失败：ID为空");
            return R.fail("ID不能为空");
        }

        // 查询记录是否存在
        LawyerExpertiseArea existingArea = expertiseAreaMapper.selectById(expertiseArea.getId());
        if (existingArea == null) {
            log.warn("编辑领域信息失败：记录不存在，ID={}", expertiseArea.getId());
            return R.fail("记录不存在");
        }

        // 设置更新时间
        expertiseArea.setUpdatedTime(new Date());

        boolean result = this.updateById(expertiseArea);
        if (result) {
            log.info("编辑领域信息成功，ID={}", expertiseArea.getId());
            // 重新查询更新后的记录
            LawyerExpertiseArea updatedArea = expertiseAreaMapper.selectById(expertiseArea.getId());
            return R.data(updatedArea != null ? updatedArea : expertiseArea);
        }
        return R.fail("修改失败");
    }

    @Override
    public R<Boolean> deleteExpertiseArea(Integer id) {
        log.info("LawyerExpertiseAreaServiceImpl.deleteExpertiseArea?id={}", id);

        // 参数校验
        if (id == null) {
            log.warn("删除领域信息失败：ID为空");
            return R.fail("ID不能为空");
        }

        // 查询记录是否存在
        LawyerExpertiseArea existingArea = expertiseAreaMapper.selectById(id);
        if (existingArea == null) {
            log.warn("删除领域信息失败：记录不存在，ID={}", id);
            return R.fail("记录不存在");
        }

        boolean result = this.removeById(id);
        if (result) {
            log.info("删除领域信息成功，ID={}", id);
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @Override
    public R<List<LawyerExpertiseArea>> getList(LawyerExpertiseArea expertiseArea) {
        log.info("LawyerExpertiseAreaServiceImpl.getList?expertiseArea={}", expertiseArea);
        List<LawyerExpertiseArea> list = QueryBuilder.of(expertiseArea)
                .likeFields("expertiseAreaInfo")  // 领域信息支持模糊查询
                .build()
                .list(this);
        return R.data(list);
    }

    @Override
    public R<IPage<LawyerExpertiseArea>> getPage(LawyerExpertiseArea expertiseArea, int page, int size) {
        log.info("LawyerExpertiseAreaServiceImpl.getPage?expertiseArea={},page={},size={}", expertiseArea, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerExpertiseArea> pageResult = QueryBuilder.of(expertiseArea)
                .likeFields("expertiseAreaInfo")  // 领域信息支持模糊查询
                .page(pageNum, pageSize)
                .build()
                .page(this);
        return R.data(pageResult);
    }
}

