package com.usky.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.datascope.context.DataScopeContextHolder;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.system.domain.HceReview;
import com.usky.system.mapper.HceReviewMapper;
import com.usky.system.service.HceReviewService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.system.service.vo.HceReviewRequestVO;
import com.usky.system.service.vo.HceReviewScoreVO;
import org.apache.tomcat.jni.Local;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.function.LongToIntFunction;

/**
 * <p>
 * 帮助中心建议反馈表 服务实现类
 * </p>
 *
 * @author han
 * @since 2024-08-07
 */
@Service
public class HceReviewServiceImpl extends AbstractCrudService<HceReviewMapper, HceReview> implements HceReviewService {

    @Override
    public void add(HceReview hceReview){
        String hceCode = "hce-"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        hceReview.setHceCode(hceCode);
        hceReview.setStatus(0);
        hceReview.setCreateBy(SecurityUtils.getUsername());
        hceReview.setCreateTime(LocalDateTime.now());
        hceReview.setDeptId(Integer.parseInt(String.valueOf(SecurityUtils.getLoginUser().getSysUser().getDeptId())));
        hceReview.setTenantId(SecurityUtils.getTenantId());

        this.save(hceReview);
    }

    @Override
    public void edit(HceReview hceReview){
        hceReview.setStatus(1);
        hceReview.setHandleBy(SecurityUtils.getUsername());
        hceReview.setHandleTime(LocalDateTime.now());
        hceReview.setUpdateBy(SecurityUtils.getUsername());
        hceReview.setUpdateTime(LocalDateTime.now());

        this.updateById(hceReview);
    }

    @Override
    public void remove(Integer id){
        if(id == null || id == 0){
            throw new BusinessException("栏目id不能为空或者等于0");
        }

        this.removeById(id);
    }

    @Override
    @DataScope
    public CommonPage<HceReview> page(HceReviewRequestVO requestVO){
        IPage<HceReview> page = new Page<>(requestVO.getPageNum(),requestVO.getPageSize());
        LambdaQueryWrapper<HceReview> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotBlank(requestVO.getHceCode()),HceReview::getHceCode,requestVO.getHceCode())
                .like(StringUtils.isNotBlank(requestVO.getReviewName()),HceReview::getReviewName,requestVO.getReviewName())
                .between(StringUtils.isNotBlank(requestVO.getStartTime())&&StringUtils.isNotBlank(requestVO.getEndTime()),HceReview::getCreateTime,requestVO.getStartTime(),requestVO.getEndTime())
                .eq(requestVO.getStatus() != null,HceReview::getStatus,requestVO.getStatus())
                .apply(DataScopeContextHolder.getDataScopeSql())
                .orderByDesc(HceReview::getId);
        page = this.page(page,queryWrapper);

        return new CommonPage<>(page.getRecords(),page.getTotal(),requestVO.getPageSize(),requestVO.getPageNum());
    }

    @Override
    public void score(HceReviewScoreVO scoreVO){

        LambdaQueryWrapper<HceReview> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(HceReview::getHceCode,scoreVO.getHceCode());
        HceReview hceReview = this.getOne(queryWrapper);
        if(Objects.isNull(hceReview.getScore())){

        }else{
            throw new BusinessException("您已参与评分，谢谢！");
        }

        hceReview.setStatus(2);
        hceReview.setScore(scoreVO.getScore());
        hceReview.setAppraiseContent(scoreVO.getAppraiseContent());
        this.updateById(hceReview);
    }

}
