package com.njcn.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.system.enums.SystemResponseEnum;
import com.njcn.system.mapper.PqDashboardPageMapper;
import com.njcn.system.pojo.param.PqDashboardPageParam;
import com.njcn.system.pojo.po.PqDashboardPage;
import com.njcn.system.pojo.vo.PqDashboardPageVO;
import com.njcn.system.service.PqDashboardPageService;
import com.njcn.user.pojo.constant.HomePageState;
import com.njcn.web.factory.PageFactory;
import com.njcn.web.utils.RequestUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 
 * Description:
 * Date: 2025/05/23  下午 3:15【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
public class PqDashboardPageServiceImpl extends ServiceImpl<PqDashboardPageMapper, PqDashboardPage> implements PqDashboardPageService{


    @Override
    public Boolean addDashboard(PqDashboardPageParam pqDashboardPageParam) {
        PqDashboardPage pqDashboardPage = new PqDashboardPage();
        checkName(pqDashboardPageParam,false);

        BeanUtils.copyProperties(pqDashboardPageParam,pqDashboardPage);
        //根据前端传递状态赋值
        //pqDashboardPage.setState(0);
//        pqDashboardPage.setUserId(RequestUtil.getUserIndex());
        this.save(pqDashboardPage);
        return true;
    }

    @Override
    public Boolean updateDashboard(PqDashboardPageParam.UpdatePqDashboardPageParam updatePqDashboardPageParam) {
        PqDashboardPage pqDashboardPage = new PqDashboardPage();
        checkName(updatePqDashboardPageParam,true);
        BeanUtils.copyProperties(updatePqDashboardPageParam,pqDashboardPage);
        this.updateById(pqDashboardPage);

        return true;
    }

    @Override
    public PqDashboardPageVO queryById(String id) {
        PqDashboardPageVO pqDashboardPageVO = new PqDashboardPageVO();
        PqDashboardPage byId = this.getById(id);
        BeanUtils.copyProperties(byId,pqDashboardPageVO);
        return pqDashboardPageVO;
    }

    @Override
    public Page<PqDashboardPageVO> queryPage(PqDashboardPageParam.PqDashboardPageQueryParam param) {
        QueryWrapper<PqDashboardPage> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(param.getSearchBeginTime()) && Objects.nonNull(param.getSearchEndTime())) {
            queryWrapper.between("A.Create_Time", param.getSearchBeginTime(), param.getSearchEndTime());
        }
        if(Objects.nonNull(param.getSearchValue())){
            queryWrapper.like("A.page_name",param.getSearchValue());
        }
//        queryWrapper.orderByAsc("A.sort");
        return this.baseMapper.page(new Page<>(PageFactory.getPageNum(param), PageFactory.getPageSize(param)), queryWrapper);
    }

    @Override
    public Boolean activatePage(String id,Integer state) {
        return this.lambdaUpdate().set(PqDashboardPage::getState,state).eq(PqDashboardPage::getId,id).update();
    }

    @Override
    public Boolean scopePage(String id, String userId) {
        return this.lambdaUpdate().set(PqDashboardPage::getUserId,userId).eq(PqDashboardPage::getId,id).update();
    }

    @Override
    public PqDashboardPageVO queryByPagePath(String pagePath) {
        List<String> userList = Arrays.asList("0",RequestUtil.getUserIndex());
        LambdaQueryWrapper<PqDashboardPage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PqDashboardPage::getPagePath,pagePath).in(PqDashboardPage::getUserId,userList);
        PqDashboardPage result = this.getOne(queryWrapper);
        if (Objects.isNull(result)) {
            return null;
        }
        PqDashboardPageVO vo = new PqDashboardPageVO();
        BeanUtils.copyProperties(result,vo);
        return vo;
    }

    @Override
    public List<PqDashboardPage> getDashboardPageByUserId(String id,boolean state) {
        List<String> userList = new ArrayList<>();
        userList.add(id);
        userList.add(HomePageState.DEFAULT_USER_ID);
        if (state) {
            return this.lambdaQuery().in(PqDashboardPage::getUserId,userList).eq(PqDashboardPage::getState,1).orderByAsc(PqDashboardPage::getSort).list();
        } else {
            return this.lambdaQuery().in(PqDashboardPage::getUserId,userList).orderByAsc(PqDashboardPage::getSort).list();
        }
    }



    /**
     * 校验参数，检查是否存在相同编码的字典类型
     */
    private void checkName(PqDashboardPageParam pqDashboardPageParam, boolean isExcludeSelf) {
        LambdaQueryWrapper<PqDashboardPage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(PqDashboardPage::getPageName, pqDashboardPageParam.getPageName())
                .eq(PqDashboardPage::getUserId, RequestUtil.getUserIndex());
        //更新的时候，需排除当前记录
        if (isExcludeSelf) {
            if (pqDashboardPageParam instanceof PqDashboardPageParam.UpdatePqDashboardPageParam) {
                queryWrapper.ne(PqDashboardPage::getId, ((PqDashboardPageParam.UpdatePqDashboardPageParam) pqDashboardPageParam).getId());
            }
        }
        int countByAccount = this.getBaseMapper().selectCount(queryWrapper);
        //大于等于1个则表示重复
        if (countByAccount >= 1) {
            throw new BusinessException(SystemResponseEnum.PAGE_SAME_NAME);
        }
    }
}
