package com.fkzy.warn.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fkzy.warn.mapper.AuthorizationMapper;
import com.fkzy.warn.service.AuthorizationService;
import com.fkzy.warn.common.constants.AuthorizationConstants;
import com.fkzy.warn.common.request.RequestParameter;
import com.fkzy.warn.common.response.ApiResponse;
import com.fkzy.warn.model.AuditRecords;
import com.fkzy.warn.model.Authorization;
import com.fkzy.warn.model.User;
import com.fkzy.warn.service.AuditRecordsService;
import com.fkzy.warn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author zhangjing
 * @date 2023/10/16 18:17
 * @description
 */
@Service
public class AuthorizationServiceImpl extends ServiceImpl<AuthorizationMapper, Authorization> implements
    AuthorizationService {

    @Autowired
    private UserService userService;

    @Autowired
    private AuditRecordsService auditRecordsService;


    @Override
    public ApiResponse saveModel(Authorization entity, String ticket) {
        ApiResponse apiResponse = new ApiResponse();
        save(entity);
        User user = new User();
        user.setId(entity.getUserId());
        user.setAuthorizationStatus(AuthorizationConstants.STATUS_PENDING_APPLICATION);
        userService.updateModel(user);
        return apiResponse;
    }

    @Override
    public boolean updateModel(Authorization entity) {
        boolean result = this.updateById(entity);
        return result;
    }

    @Override
    public boolean deleteModel(Authorization entity) {
        boolean result = this.removeById(entity);
        
        return result;
    }

    @Override
    public Page<User> queryPage(RequestParameter parameter) {

        return userService.queryPage(parameter);
    }

    @Override
    public ApiResponse applyFor(JSONObject jsonObject, String ticket) {
        ApiResponse apiResponse = new ApiResponse();
        Integer id = jsonObject.getInteger("id");
        Integer authorizationId = jsonObject.getInteger("authorizationId");
        User sysUser = userService.getUserByTicket(ticket);

        AuditRecords auditRecords = new AuditRecords();
        auditRecords.setAuditDate(new Date());
        auditRecords.setUserId(sysUser!=null?sysUser.getId():null);
        auditRecords.setUserName(sysUser!=null?sysUser.getLegalPerson():null);
        auditRecords.setBusinessId(authorizationId);
        auditRecords.setStatusCode(AuthorizationConstants.AUDIT_STATUS_PENDING);
        auditRecordsService.save(auditRecords);
        //用户状态
        User user = new User();
        user.setId(id);
        user.setAuthorizationStatus(AuthorizationConstants.STATUS_PENDING_APPROVAL);
        userService.updateModel(user);

        return apiResponse;
    }

    @Override
    public ApiResponse approve(JSONObject jsonObject, String ticket) {
        ApiResponse apiResponse = new ApiResponse();
        String content = jsonObject.getString("content");
        String statusCode = jsonObject.getString("statusCode");

        return apiResponse;
    }
}
