package com.ruoyi.zzb.zdcx.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.zzb.zdcx.domain.Zdcx;
import com.ruoyi.zzb.zdcx.mapper.ZdcxMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Slf4j
@Service
@DataSource(value = DataSourceType.SLAVE)
public class ZdcxService {

    @Autowired
    private ZdcxMapper zdcxMapper;

    /**
     * 根据条件分页查询制度信息
     * @param reqBody
     * @return
     */
    public Page<Zdcx> getDataByPage(JSONObject reqBody){
        String docId = reqBody.getString("docId");
        String title = reqBody.getString("title");
        String publishDept = reqBody.getString("publishDept");
        String content = reqBody.getString("content");
        // 前端传入时间范围查询的 起始时间、结束时间
        String startTime = reqBody.getString("startTime");
        String endTime = reqBody.getString("endTime");
        // 查询标志  0：and，1：or
        Integer queryFlag = reqBody.getInteger("queryFlag");
        Integer pageIndex = reqBody.getInteger("pageIndex") == null? 1:reqBody.getInteger("pageIndex");
        Integer pageSize = reqBody.getInteger("pageSize") == null? 10:reqBody.getInteger("pageSize");
        // 构造查询条件
        QueryWrapper<Zdcx> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("FLAG", 1);
//        queryWrapper.eq("1", 1);
        if(StringUtils.isNotBlank(docId)){
            if (queryFlag != 0){              // or
                queryWrapper.or();
            }
            queryWrapper.like("DOC_ID", docId);
        }
        if(StringUtils.isNotBlank(title)){
            if (queryFlag != 0){
                queryWrapper.or();
            }
            queryWrapper.like("TITLE", title);
        }
        if(StringUtils.isNotBlank(publishDept)){
            if (queryFlag != 0){
                queryWrapper.or();
            }
            queryWrapper.like("PUBLISH_DEPT", publishDept);
        }
        if(StringUtils.isNotBlank(content)){
            if (queryFlag != 0){
                queryWrapper.or();
            }
            queryWrapper.like("CONTENT", content);
        }
        if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
            if (queryFlag != 0){
                queryWrapper.or();
            }
            queryWrapper.between("CREATE_TIME", DateUtils.parseDate(startTime), DateUtils.parseDate(endTime));
        }
        Page<Zdcx> page = new Page<>(pageIndex, pageSize);
        return zdcxMapper.selectPage(page, queryWrapper);
    }


    /**
     * 根据参数保存一条制度规范信息
     * @param
     * @param basePersonInfo
     * @return
     *//*
    public boolean insertStandardInfo(Zdcx zdcx, BasePersonInfo basePersonInfo){
        // 制度信息
        *//*zdcx.setDocId(reqBody.getString("docId"));
        zdcx.setTitle(reqBody.getString("title"));
        zdcx.setPublishDept(reqBody.getString("publishDept"));
        zdcx.setContent(reqBody.getString("content"));*//*
        // 录入人信息
        zdcx.setCreateUserId(basePersonInfo.getUserId());
        zdcx.setCreateUserName(basePersonInfo.getName());
        zdcx.setCreateUserPoliceNo(basePersonInfo.getPoliceNo());
        zdcx.setCreateDeptName(basePersonInfo.getDeptName());
        zdcx.setCreateDeptCode(basePersonInfo.getDeptCode());
        zdcx.setCreateParentDeptName(basePersonInfo.getParentDeptName());
        zdcx.setCreateParentDeptCode(basePersonInfo.getParentDeptCode());
        if (zdcxMapper.insert(zdcx) > 0) {
            log.info("制度规范信息保存成功");
            return true;
        }
        log.error("制度规范信息保存失败");
        return false;
    }*/


    /**
     * 根据参数保存一条制度规范信息
     * @param
     * @param
     * @return
     */
    @Transactional
    public boolean insertStandardInfo(Zdcx zdcx, SysUser user){
        // 录入人信息
        zdcx.setCreateUserId(String.valueOf(user.getUserId()));
        zdcx.setCreateUserName(user.getNickName());
        zdcx.setCreateUserPoliceNo(user.getPoliceNo());
        SysDept dept = user.getDept();
        zdcx.setCreateDeptName(dept.getDeptName());
        zdcx.setCreateDeptCode(String.valueOf(user.getDeptId()));
        zdcx.setCreateParentDeptName(dept.getParentName());
        zdcx.setCreateParentDeptCode(String.valueOf(dept.getParentId()));
        int insertRows = zdcxMapper.insert(zdcx);
        if (insertRows > 0) {
            log.info("【制度查询】制度规范信息保存成功");
            return true;
        }else {
            throw new RuntimeException("数据库【制度查询】制度规范信息记录未插入");
        }
    }

    /**
     * 根据docId（文书号）条件查询制度规范信息
     * @param docId
     * @return
     */
    public Zdcx getDataByDocId(String docId){
        if(StringUtils.isNotBlank(docId)){
            QueryWrapper<Zdcx> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("DOC_ID", docId);
            return zdcxMapper.selectOne(queryWrapper);
        }
        return null;
    }

    /**
     * 根据参数修改一条制度规范信息
     * @param
     * @param
     * @return
     */
    @Transactional
    public boolean updateStandardInfo(Zdcx zdcx){
        String docId = zdcx.getDocId();
        UpdateWrapper<Zdcx> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("DOC_ID", docId);
        int updateRows = zdcxMapper.update(zdcx, updateWrapper);
        if(updateRows > 0){
            log.info("【制度查询】制度规范信息修改成功  文书号：{}", docId);
            return true;
        } else {
            throw new RuntimeException("数据库【制度查询】制度规范信息记录未修改或不存在  文书号：" + docId);
        }
    }


    /**
     * 根据docIds（文书号集合）删除一/多条制度规范信息
     * @param docIds
     * @return
     */
    @Transactional
    public boolean delDataByDocId(List<String> docIds){
        QueryWrapper<Zdcx> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("DOC_ID", docIds);
        int delRows = zdcxMapper.delete(queryWrapper);
        if(delRows == docIds.size()){
            log.info("制度规范信息删除成功  已删除的文书号：{}，删除的数据条数：{}", docIds, delRows);
            return true;
        } else {
            throw new RuntimeException("数据库制度规范信息记录未删除或不存在  删除失败的文书号：" + docIds);
        }
    }

}
