package com.fuint.module.backendApi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fuint.common.dto.AccountInfo;
import com.fuint.common.dto.MaterialTypeDto;
import com.fuint.common.dto.MaterialTypeSearchDto;
import com.fuint.common.util.AuthUserUtil;
import com.fuint.common.util.TokenUtil;
import com.fuint.common.vo.MaterialTypeVo;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.PageResult;
import com.fuint.framework.web.ResponseObject;
import com.fuint.common.service.MaterialTypeService;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.repository.model.MtMaterialType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 材料类型表管理类controller
 *
 * Created by PZG
 * CopyRight https://www.fuint.cn
 */
@Api(tags="管理端-材料类型表相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/backendApi/material_type")
public class BackendMaterialTypeController extends BaseController {

    /**
     * 材料类型表服务接口
     */
    private MaterialTypeService materialTypeService;

    /**
     * 材料类型表列表查询
     *
     * @return 材料类型表列表
     */
    @ApiOperation(value = "材料类型表列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('material:type:list')")
    public PageResult<MaterialTypeVo> list(MaterialTypeSearchDto materialTypeSearchDto) throws BusinessCheckException {

        AccountInfo accountInfo = AuthUserUtil.get();
        if (accountInfo == null) {
            throw  new BusinessCheckException("用户未登录");
        }

        return materialTypeService.selectMaterialTypeList(materialTypeSearchDto);
    }

    /**
     * 更新材料类型表状态
     *
     * @return
     */
    @ApiOperation(value = "更新材料类型表状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('material_type:edit')")
    public ResponseObject updateStatus(@RequestBody MaterialTypeDto mtMaterialTypeDto) throws BusinessCheckException {
        AccountInfo accountInfo = AuthUserUtil.get();
        if (accountInfo == null) {
            return getFailureResult(1001, "请先登录");
        }
        //校验id合法性
        MtMaterialType rawMtMaterialType = materialTypeService.queryMaterialTypeById(mtMaterialTypeDto.getId());
        if (rawMtMaterialType == null) {
            return getFailureResult(201,"数据不存在");
        }
        MtMaterialType newMtMaterialType = new MtMaterialType();
        //只允许更新这三个字段
        newMtMaterialType.setName(mtMaterialTypeDto.getName());
        newMtMaterialType.setProject(mtMaterialTypeDto.getProject());
        newMtMaterialType.setEnable(mtMaterialTypeDto.getEnable());
        materialTypeService.updateMaterialType(newMtMaterialType);

        return getSuccessResult(true);
    }

    /**
     * 保存材料类型表
     *
     * @return
     */
    @ApiOperation(value = "保存材料类型表")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('material:type:add')")
    public ResponseObject saveHandler(@RequestBody MaterialTypeDto mtMaterialTypeDto) throws BusinessCheckException {

        AccountInfo accountInfo = AuthUserUtil.get();
        if (accountInfo == null) {
            return getFailureResult(1001, "请先登录");
        }
        MtMaterialType mtMaterialType = new MtMaterialType();
        BeanUtils.copyProperties(mtMaterialTypeDto,mtMaterialType);
        return materialTypeService.addMaterialType(mtMaterialType);
    }

    /**
     * 根据id删除材料类型数据
     *
     * @return 结果
     */
    @ApiOperation(value = "删除材料类型数据")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('material_type:delete')")
    public ResponseObject deleteHandler(@PathVariable Long id) throws BusinessCheckException {

        AccountInfo accountInfo = AuthUserUtil.get();
        if (accountInfo == null) {
            return getFailureResult(1001, "请先登录");
        }
        return materialTypeService.deleteHandler(id);
    }

}
