package cn.com.ty.lift.ud.operation.service.impl;

import cn.com.ty.lift.ud.operation.mapper.OtherSettingMapper;
import cn.com.ty.lift.ud.operation.service.IOtherSettingService;
import cn.com.ty.lift.ud.common.NotExamineDto;
import cn.com.ty.lift.ud.operation.mapper.entity.OtherSettingEntity;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.com.xwy.util.collection.ListUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 其他设置 -ServiceImpl
 *
 * @author Xson
 * @date 2020-05-16
 */
@Service
@Transactional
public class OtherSettingServiceImpl extends ServiceImpl<OtherSettingMapper,OtherSettingEntity>
    implements IOtherSettingService {

    public static final int EXAMINEFLAG = 1;

    @Autowired
    private OtherSettingMapper otherSettingMapper;

    /**
     * 新增提交
     *
     * @param entity
     * @return
     */
    @Override
    public RestResponse create(OtherSettingEntity entity) {
        Long id = entity.getId();
        //同时只能存在一个待审核的版本
        RestResponse allStay = getAllStay();
        List<OtherSettingEntity> entityList = (List<OtherSettingEntity>) allStay.getData();
        if(ListUtil.isNotEmpty(entityList)){
            return RestResponse.fail("已存在待审核的版本,不能重复提交！");
        }else{
            if (null == id) {
                //直接点的提交按钮
                entity.setExamineFlag(0);
                int insert = otherSettingMapper.insert(entity);
                if (insert == 1) {
                    RestResponse stay = getAllStay();
                    List<OtherSettingEntity> stayList = (List<OtherSettingEntity>) stay.getData();
                    return RestResponse.success(stayList.get(0), "提交成功");
                } else {
                    return RestResponse.fail("提交失败");
                }
            } else {
                //先保存再提交,修改状态为未审核 0
                return submit(id);
            }
        }
    }

    /**
     * 提交操作
     *
     * @param id
     * @return
     */
    @Override
    public RestResponse submit(Long id) {
        //同时只能存在一个待审核的版本
        RestResponse allStay = getAllStay();
        List<OtherSettingEntity> entityList = (List<OtherSettingEntity>) allStay.getData();
        if(ListUtil.isNotEmpty(entityList)){
            return RestResponse.fail("已存在待审核的版本,不能重复提交！");
        }else{
            //提交
            int submit = otherSettingMapper.submit(id);
            if (submit == 1) {
                RestResponse stay = getAllStay();
                List<OtherSettingEntity> stayList = (List<OtherSettingEntity>) stay.getData();
                return RestResponse.success(stayList.get(0), "提交成功");
            } else {
                return RestResponse.fail(null, "提交失败");
            }
        }

    }


    /**
     * 查询所有待审核的
     * @return
     */
    @Override
    public RestResponse getAllStay() {
        List<OtherSettingEntity> allStayList = otherSettingMapper.getAllStay();
        if(ListUtil.isNotEmpty(allStayList)){
            return RestResponse.success(allStayList,"查询成功");
        }else{
            return RestResponse.fail("暂无数据");
        }
    }

    /**
     * 查询所有待提交的
     * @return
     */
    @Override
    public RestResponse getAllCommit() {
        List<OtherSettingEntity> allCommit = otherSettingMapper.getAllCommit();
        if(ListUtil.isNotEmpty(allCommit)){
            return RestResponse.success(allCommit,"查询成功");
        }else{
            return RestResponse.fail("暂无数据");
        }
    }

    /**
     * 查询历史版版本（最新版本的上一个版本）
     * @return
     */
    @Override
    public RestResponse getHistoryVersion() {
        OtherSettingEntity historyVersion = otherSettingMapper.getHistoryVersion();
        if(null != historyVersion){
            return RestResponse.success(historyVersion,"查询成功");
        }else{
            return RestResponse.fail("暂无数据");
        }
    }

    /**
     * 查询最新的版本
     * @return
     */
    @Override
    public RestResponse getLatestVersion() {
        OtherSettingEntity latestVersion = otherSettingMapper.getLatestVersion();
        if(null != latestVersion){
            return RestResponse.success(latestVersion,"查询成功");
        }else{
            return RestResponse.fail("暂无数据");
        }
    }

    /**
     * 审批
     * @param neDto
     * @return
     */
    @Override
    public RestResponse examineById(NotExamineDto neDto) {
        if(EXAMINEFLAG == neDto.getExamineFlag()){
            //查询最新的版本
            OtherSettingEntity latestVersion = otherSettingMapper.getLatestVersion();
            if(null != latestVersion){
                Integer versionId = latestVersion.getVersionId();
                if(versionId != null){
                    versionId = versionId + 1;
                    //设置版本号
                    otherSettingMapper.updateVersionId(versionId,neDto.getId());
                    return RestResponse.success(null,"审核成功");
                }else{
                    return RestResponse.fail("上一个版本无版本号,审核失败");
                }
            }else{
                //审核第一条数据，版本ID直接设为1
                int insert = otherSettingMapper.updateVersionId(1,neDto.getId());
                if(insert == 1){
                    return RestResponse.success(null,"审核成功");
                }else{
                    return RestResponse.fail("审核失败");
                }
            }
        }else{
            //修改状态为 驳回，并加上驳回理由
            otherSettingMapper.updateInfoById(neDto.getId(),neDto.getNotExamineReason());
            return RestResponse.fail("审批驳回");
        }
    }
}
