package com.xhpc.general.service;

import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.general.domain.HelpEntity;
import com.xhpc.general.mapper.XhpcHelpMapper;
import com.xhpc.general.util.lengthCheckUtil.HelpEntityLengthCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 帮助Service业务层处理
 * program: ruoyi
 * User: HongYun
 * Date:2021-08-02 11
 */
@Service
public class XhpcHelpServiceImpl implements IXhpcHelpService{

    @Autowired
    XhpcHelpMapper xhpcHelpMapper;
    @Override
    @Transactional
    public AjaxResult deleteHelpItem(Long helpId) {

        int res=xhpcHelpMapper.deleteHelpItem(helpId);
        if(res==0){
            return AjaxResult.error("删除失败");
        }else {
            return AjaxResult.success();
        }
    }


    @Override
    @Transactional
    public AjaxResult insertHelpItem(String title, String content, Integer type) {
        //Ensuring parameter is valid
        title=title.trim();
        if(title.equals("")){
            return AjaxResult.error("标题不能为空");
        }
        content=content.trim();
        if(content.equals("")){
            return AjaxResult.error("内容不能为空");
        }
        //Checking title's length.
        if(!HelpEntityLengthCheckUtil.checkTitleL(title.length())){
            return AjaxResult.error("标题的长度不能大于"+HelpEntityLengthCheckUtil.getTitleLL());
        }
        //Ensuring the title is unique.
        int res1 = xhpcHelpMapper.countSameTypeTitle(type, title);
        if (res1 != 0) {
            return AjaxResult.error("此类型已有此标题");
        }

        int res2=xhpcHelpMapper.insertHelpItem(title, content, type);
        if(res2==0){
            return AjaxResult.error("插入失败");
        }
        return AjaxResult.success();
    }


    @Override
    @Transactional
    public AjaxResult updateHelpItem(Long helpId, String title, String content, Integer type) {
        //Ensuring parameter is valid
        title=title.trim();
        if(title.equals("")){
            return AjaxResult.error("标题不能为空");
        }
        content=content.trim();
        if(content.equals("")){
            return AjaxResult.error("内容不能为空");
        }

        //Checking the title's length.
        if(!HelpEntityLengthCheckUtil.checkTitleL(title.length())){
            return AjaxResult.error("标题的长度不能大于"+HelpEntityLengthCheckUtil.getTitleLL());
        }
        HelpEntity helpEntity=xhpcHelpMapper.selectHelpItem(helpId);
        if(!helpEntity.getTitle().equals(title)) {
            int res1 = xhpcHelpMapper.countSameTypeTitle(type, title);
            if (res1 != 0) {
                return AjaxResult.error("此类型已有此标题");
            }
        }
        int res2=xhpcHelpMapper.updateHelpItem(helpId, title, type, content);
        if(res2==0){
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success();
    }


    @Override
    @Transactional
    public List<HelpEntity> selectHelpItems(String title, String createBy, Integer type) {

        return xhpcHelpMapper.selectHelpItems(title, createBy, type);
    }

    @Override
    public AjaxResult selectHelpItem(Long helpId) {

        return AjaxResult.success(xhpcHelpMapper.selectHelpItem(helpId));
    }


    @Override
    public List<Map<String, Object>> selectTitleId(Integer type) {

        return xhpcHelpMapper.selectTitleId(type);
    }

    @Override
    public Map<String, Object> selectDetails(Long helpId) {

        return xhpcHelpMapper.selectDetails(helpId);
    }
}
