package com.ruoyi.bst.agreement.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.agreement.domain.Agreement;
import com.ruoyi.bst.agreement.domain.AgreementQuery;
import com.ruoyi.bst.agreement.domain.AgreementVO;
import com.ruoyi.bst.agreement.domain.enums.AgreementContentType;
import com.ruoyi.bst.agreement.domain.enums.AgreementType;
import com.ruoyi.bst.agreement.mapper.AgreementMapper;
import com.ruoyi.bst.agreement.service.AgreementService;
import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.system.user.service.UserService;

/**
 * 协议Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@Service
public class AgreementServiceImpl implements AgreementService
{
    @Autowired
    private AgreementMapper agreementMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;

    /**
     * 查询协议
     *
     * @param id 协议主键
     * @return 协议
     */
    @Override
    public AgreementVO selectAgreementById(Long id)
    {
        return agreementMapper.selectAgreementById(id);
    }

    /**
     * 查询协议列表
     *
     * @param agreement 协议
     * @return 协议
     */
    @Override
    public List<AgreementVO> selectAgreementList(AgreementQuery agreement)
    {
        return agreementMapper.selectAgreementList(agreement);
    }

    /**
     * 新增协议
     *
     * @param agreement 协议
     * @return 结果
     */
    @Override
    public int insertAgreement(Agreement agreement)
    {
        contentTypeAreaCheck(agreement);
        agreement.setCreateTime(DateUtils.getNowDate());
        Integer result = transactionTemplate.execute(status -> {
            // 创建设备
            int insert = agreementMapper.insertAgreement(agreement);
            ServiceUtil.assertion(insert != 1,"新增协议失败");

            // 后校验
            AgreementVO vo = this.selectAgreementById(agreement.getId());
            distinct(vo);

            return insert;

        });
        return result==null? 0 : result;
    }

    /**
     * 修改协议
     *
     * @param agreement 协议
     * @return 结果
     */
    @Override
    public int updateAgreement(Agreement agreement)
    {
        contentTypeAreaCheck(agreement);
        agreement.setUpdateTime(DateUtils.getNowDate());
        if (agreement.getContentType().equals(AgreementContentType.TEMPLATE.getCode())){
            agreement.setAreaId(null);
        }
        Integer result = transactionTemplate.execute(status -> {

            // 更新主表
            int update = agreementMapper.updateAgreement(agreement);
            ServiceUtil.assertion(update != 1, "更新设备失败");

            // 后校验
            AgreementVO vo = this.selectAgreementById(agreement.getId());
            distinct(vo);

            return update;
        });
        return result == null ? 0 : result;
    }

    private void distinct(AgreementVO agreement) {
        AgreementQuery query = new AgreementQuery();
        query.setContentType(agreement.getContentType());
        query.setId(agreement.getId());
        if (agreement.getAreaId() != null) {
            query.setAreaId(agreement.getAreaId());
        }
        query.setAgreementType(agreement.getAgreementType());
        ServiceUtil.assertion(agreementMapper.selectDistinct(query) > 0,"当前类型协议已存在");
    }

    private void contentTypeAreaCheck(Agreement agreement) {
        ServiceUtil.assertion(agreement.getContentType().equals(AgreementContentType.TEXT.getCode())&&agreement.getAreaId()==null,"协议运营区不允许为空");
    }
    /**
     * 批量删除协议
     *
     * @param ids 需要删除的协议主键
     * @return 结果
     */
    @Override
    public int deleteAgreementByIds(List<Long> ids)
    {
        return agreementMapper.deleteAgreementByIds(ids);
    }

    /**
     * 删除协议信息
     *
     * @param id 协议主键
     * @return 结果
     */
    @Override
    public int deleteAgreementById(Long id)
    {
        return agreementMapper.deleteAgreementById(id);
    }


    @Override
    public int copyTemplate(AgreementType type, Long areaId) {
        if (type == null || areaId == null) {
            return 0;
        }

        // 查询模板
        AgreementQuery query = new AgreementQuery();
        query.setAgreementType(type.getCode());
        query.setContentType(AgreementContentType.TEMPLATE.getCode());
        AgreementVO template = this.selectOne(query);
        ServiceUtil.assertion(template == null, "协议模板不存在");

        Agreement data = new Agreement();
        data.setAreaId(areaId);
        data.setTitle(template.getTitle());
        data.setBrief(template.getBrief());
        data.setContent(template.getContent());
        data.setAgreementType(template.getAgreementType());
        data.setContentType(AgreementContentType.TEXT.getCode());
        data.setDuration(template.getDuration());
        return this.insertAgreement(data);
    }

    private AgreementVO selectOne(AgreementQuery query) {
        PageHelper.startPage(1, 1);
        List<AgreementVO> list = agreementMapper.selectAgreementList(query);
        return CollectionUtils.firstElement(list);
    }
}
