package com.goafanti.standard.controller;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.model.StandardDocument;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.standard.bo.InputStandardList;
import com.goafanti.standard.bo.OutStandardDocument;
import com.goafanti.standard.service.StandardService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/admin/standard")
public class StandardApiController extends CertifyApiController {

    @Resource
    private StandardService standardService;

    /**
     * 标准列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(InputStandardList in) {
        Result res = new Result<Pagination<OutStandardDocument>>();
        return res.data(standardService.list(in));
    }
    /**
     * 标准详情
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public Result get(StandardDocument in) {
        Result res = res();
        return res.data(standardService.get(in));
    }

    /**
     * 新增标准
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(StandardDocument in) {
        Result res = res();
        if (!TokenManager.hasRole(AFTConstants.SUPERADMIN)) {
            res.error(buildError("操作权限不正确。"));
            return res;
        }
        if(standardService.inspectNumber(in)){
            res.error(buildError("标准编号已存在。"));
            return res;
        }
        return res.data(standardService.add(in));
    }

    /**
     * 删除标准
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(StandardDocument in) {
        Result res = res();
        if (!TokenManager.hasRole(AFTConstants.SUPERADMIN)) {
            res.error(buildError("操作权限不正确。"));
            return res;
        }
        return res.data(standardService.delete(in));
    }

    /**
     * 更新标准
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(StandardDocument in) {
        Result res = res();
        if (!TokenManager.hasRole(AFTConstants.SUPERADMIN)) {
            res.error(buildError("操作权限不正确。"));
            return res;
        }
        if(standardService.inspectNumber(in)){
            res.error(buildError("标准编号已存在。"));
            return res;
        }
        return res.data(standardService.update(in));
    }

    /**
     * 图片上传
     */
    @RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
    public Result uploadPicture(HttpServletRequest req, String sign) {
        Result res = new Result();
        AttachmentType attachmentType = AttachmentType.getField(sign);

        if (attachmentType == AttachmentType.STANDARD_DOCUMENT
        ) {
            res.setData(handleFiles(res, "/standard/", false, req, sign, ""));
        } else {
            res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "图片"));
        }
        return res;
    }




}
