package com.xhpc.general.service;

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

import java.util.List;

/**
 * 协议管理Service业务层处理
 * program: ruoyi
 * User: HongYun
 * Date:2021-08-03 09
 */
@Service
public class XhpcAgreementServiceImpl implements IXhpcAgreementService {

    @Autowired
    XhpcAgreementMapper xhpcAgreementMapper;

    @Override
    @Transactional
    public AjaxResult deleteAgreementItem(Long agreementId,String tenantId) {
        int res=xhpcAgreementMapper.deleteAgreementItem(agreementId,tenantId);
        if(res==0){
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success();
    }


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

        //Check parameter's length.
        if(!AgreementEntityLengthCheckUtil.checkTitleL(title.length())){
            return AjaxResult.error("标题的长度大于"+AgreementEntityLengthCheckUtil.getTitleL());
        }

        //Ensuring the title which you wanna insert is unique.

        int res1 = xhpcAgreementMapper.countSameTypeTitle(type, title,tenantId);
        if (res1 != 0) {
            return AjaxResult.error("此协议类型已有此标题");
        }

        int res2=xhpcAgreementMapper.insertAgreementItem(title, type, content,tenantId);
        if(res2==0){
            return AjaxResult.error("增加失败");
        }
        return AjaxResult.success();
    }


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

        //Check parameter's length.
        if(!AgreementEntityLengthCheckUtil.checkTitleL(title.length())){
            return AjaxResult.error("标题长度大于"+AgreementEntityLengthCheckUtil.getTitleL());
        }
        XhpcAgreement xhpcAgreement=xhpcAgreementMapper.selectAgreementItem(agreementId);
        if(!xhpcAgreement.getTitle().equals(title)) {
            int res = xhpcAgreementMapper.countSameTypeTitle(type,title,tenantId);
            if (res != 0) {
                return AjaxResult.error("此协议类型已有此标题");
            }
        }
        int res1=xhpcAgreementMapper.updateAgreementItem(agreementId, title, type, content,tenantId);
        if(res1==0){
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success();
    }


    @Override
    public List<XhpcAgreement> selectAgreementItems(String title, String createBy, Integer type,String tenantId) {

        return xhpcAgreementMapper.selectAgreementItems(title, createBy, type,tenantId);
    }


    @Override
    public XhpcAgreement selectAgreementItem(Long agreementId) {

        return xhpcAgreementMapper.selectAgreementItem(agreementId);
    }


    @Override
    public XhpcAgreement selectContent(Integer type,String tenantId) {

        return xhpcAgreementMapper.selectContentType(type,tenantId);
    }
}
