package com.ruoyi.web.work.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.work.api.util.AppUtil;
import com.ruoyi.web.work.domain.Packages;
import com.ruoyi.web.work.domain.Project;
import com.ruoyi.web.work.domain.User;
import com.ruoyi.web.work.domain.dto.PackagesDto;
import com.ruoyi.web.work.domain.dto.RejectDto;
import com.ruoyi.web.work.domain.vo.PackagesListVo;
import com.ruoyi.web.work.mapper.PackagesMapper;
import com.ruoyi.web.work.service.IPackagesService;
import com.ruoyi.web.work.service.IProjectService;
import com.ruoyi.web.work.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.util.List;

/**
 * @author lsw
 * @date 2024-04-19
 */
@Service
public class PackagesServiceImpl extends ServiceImpl<PackagesMapper, Packages> implements IPackagesService {
    @Autowired
    private PackagesMapper packagesMapper;

    @Autowired
    private IProjectService projectService;

    @Autowired
    IUserService userService;

    @Override
    public List<Packages> selectList(Packages packages) {
        return packagesMapper.selectList(packages);
    }

    @Override
    public List<PackagesListVo> selectPackagesList(Packages packages) {
        packages.setUserId(AppUtil.getUser().getId());
        return packagesMapper.selectPackagesList(packages);
    }

    @Override
    public AjaxResult add(Packages packages) throws ServerException {
        User user = userService.getById(AppUtil.getUser().getId());
        if (user.getIsContract() == 0) {
            return AjaxResult.error("请先签约");
        }
        if (StringUtils.isEmpty(user.getBankName()) || StringUtils.isEmpty(user.getBankAccount())) {
            return AjaxResult.error(7777,"请先完善结算信息");
        }
        Project project = projectService.getById(packages.getProjectId());
        if (project == null || project.getState() == 1) {
            return AjaxResult.error("项目不存在或被停用");
        }
        packages.setAudit(0);
        packages.setState(0);
        packages.setUserId(AppUtil.getUser().getId());
        Packages check = packagesMapper.check(packages);
        if (check != null) {
            return AjaxResult.error("请勿重复接包");
        }
        if (!save(packages)) {
            throw new ServerException("接包失败,请联系平台");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult voucher(PackagesDto dto) throws ServerException {
        Packages packages = getById(dto.getId());
        if (packages == null || !AppUtil.getUser().getId().equals(packages.getUserId())) {
            return AjaxResult.error("包不存在或者非法操作");
        }
        if (packages.getAudit() == 2) {
            return AjaxResult.error("你已上传凭证");
        }
        packages.setAudit(1);
        packages.setVoucher(dto.getVoucher());
        if (!updateById(packages)) {
            throw new ServerException("更新凭证失败,请联系平台");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult success(Long id) throws ServerException {
        Packages packages = getById(id);
        if (packages == null || !packages.getCompanyId().equals(SecurityUtils.getLoginUser().getUser().getCompanyId())) {
            return AjaxResult.error("当前接包不存在或非法操作");
        }
        if (packages.getAudit() != 1) {
            return AjaxResult.error("当前接包凭证不能被审核");
        }
        //凭证审核通过，接包完成
        packages.setAudit(2);
        packages.setState(1);
        if (!updateById(packages)) {
            throw new ServerException("更新凭证失败,请联系平台");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult reject(RejectDto dto) throws ServerException {
        Packages packages = getById(dto.getId());
        if (packages == null || !packages.getCompanyId().equals(SecurityUtils.getLoginUser().getUser().getCompanyId())) {
            return AjaxResult.error("当前接包不存在或非法操作");
        }
        if (packages.getAudit() == 2) {
            return AjaxResult.error("当前接包凭证不能被审核");
        }
        packages.setMsg(dto.getMsg());
        packages.setState(0);
        packages.setAudit(3);
        if (!updateById(packages)) {
            throw new ServerException("更新凭证失败,请联系平台");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult remove(Long id) throws ServerException {
        Packages packages = getById(id);
        if (packages == null || !packages.getCompanyId().equals(SecurityUtils.getLoginUser().getUser().getCompanyId())) {
            return AjaxResult.error("当前接包不存在或非法操作");
        }
        if (!removeById(id)) {
            throw new ServerException("更新接包失败,请联系平台");
        }
        return AjaxResult.success();
    }
}
