package cn.com.ty.lift.ud.applyExpert.controller;

import cn.com.ty.lift.ud.applyExpert.service.IApplyExpertService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ty.lift.ud.applyExpert.controller.query.ApplyExpertQuery;
import cn.com.ty.lift.ud.applyExpert.mapper.entity.ApplyExpert;

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.userAccount.mapper.entity.UserAccount;
import cn.com.ty.lift.ud.userAccount.service.IUserAccountService;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import cn.com.xwy.boot.web.dto.RestResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 专家申请表API
 * 
 * @author Xson
 * @date 2020-05-16
 */
@Api(tags = "专家申请表API")
@RestController
@RequestMapping("/api/applyExperts")
public class ApplyExpertController extends BaseController<ApplyExpert, ApplyExpertQuery> {

    private final Logger log = LoggerFactory.getLogger(ApplyExpertController.class);

    private @Autowired IApplyExpertService applyExpertService;

    private @Autowired IUserAccountService iUserAccountService;

    private @Autowired IUserInfoService userInfoService;

    @Override
    protected IService<ApplyExpert> getService() {
        return applyExpertService;
    }

    @ApiOperation(value = "审批 1:通过 0:驳回")
    @PostMapping("/examine")
    public RestResponse examine(@RequestBody ApplyExpert neDto) {
        return applyExpertService.examineById(neDto);
    }

    // 新建
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    public RestResponse<ApplyExpert> create(@RequestBody ApplyExpert entity) {
        UserAccount byId = iUserAccountService.getById(entity.getUserId());
        if (byId.getExpertFlag() == 1) {
            return RestResponse.fail(null, "您已经是专家");
        }
        entity.setCheckFlag(2);
        boolean saveFlag = getService().save(entity);
        if (saveFlag)
            return RestResponse.success(null, "Created successfully");
        else
            return RestResponse.fail("Created unsuccessfully");
    }

    // 根据条件查询
    @PostMapping("/query-page")
    @ApiOperation(value = "分页查询")
    public RestResponse getCondition(@RequestBody ApplyExpertQuery request) {
        return applyExpertService.listExpert(request);
    }

    // 根据userId查详情
    @GetMapping("/queryByUserId/{userId}")
    @ApiOperation(value = "获取详情")
    public RestResponse getByUserId(@PathVariable Long userId) {
        ApplyExpert one = applyExpertService.getOne(new QueryWrapper<ApplyExpert>().eq("user_id", userId));
        return RestResponse.success(one, "Queried successfully");
    }

    // 修改
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public RestResponse update(@RequestBody ApplyExpert request) {
        ApplyExpert byId = applyExpertService.getById(request.getId());
        if (byId.getCheckFlag() == 0) {
            request.setCheckFlag(2);
        }
        boolean updateFlag = getService().updateById(request);
        if (updateFlag)
            return RestResponse.success(null, "Updated successfully");
        else
            return RestResponse.fail("Updated unsuccessfully");
    }

}