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

import cn.com.ty.lift.ud.operation.controller.query.VipPrerogativeQuery;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictDto;
import cn.com.ty.lift.ud.tableDict.service.ITableDictService;
import cn.com.ty.lift.ud.common.NotExamineDto;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictVo;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.com.xwy.util.collection.ListUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ty.lift.ud.operation.mapper.entity.VipPrerogativeEntity;
import cn.com.ty.lift.ud.operation.service.IVipPrerogativeService;

import cn.com.ty.lift.ud.common.BaseController;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户特权 API
 *
 * @author Xson
 * @date 2020-05-04
 */
@Api(tags = "用户特权 API-小陶")
@Deprecated
@RestController
@RequestMapping("/api/vipPrerogatives")
public class VipPrerogativeController extends BaseController<VipPrerogativeEntity, VipPrerogativeQuery> {

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

    private @Autowired
    IVipPrerogativeService vipPrerogativeService;

    private @Autowired
    ITableDictService iTableDictService;

    @Override
    protected IService<VipPrerogativeEntity> getService() {
        return vipPrerogativeService;
    }

    /**
     * 新增提交
     *
     * @param entity
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增提交")
    @Override
    public RestResponse<VipPrerogativeEntity> create(@RequestBody VipPrerogativeEntity entity) {
        if(null == entity.getPrerogative()){
            return RestResponse.fail("特权名称不能为空");
        }
        return vipPrerogativeService.create(entity);
    }

    /**
     * 新增保存
     *
     * @param entity
     * @return
     */
    @PostMapping("/insert-save")
    @ApiOperation(value = "新增保存")
    public RestResponse createSave(@RequestBody VipPrerogativeEntity entity) {
        if(null == entity.getPrerogative()){
            return RestResponse.fail("特权名称不能为空");
        }
        entity.setExamineFlag(3);
        boolean saveFlag = getService().save(entity);
        if (saveFlag) {
            return RestResponse.success(entity.getId(), "保存成功");
        } else {
            return RestResponse.fail("保存失败");
        }
    }

    /**
     * 提交操作
     *
     * @param id
     * @return
     */
    @PostMapping("/submit/{id}")
    @ApiOperation(value = "提交操作")
    public RestResponse submit(@PathVariable("id") Long id) {
        if(null == id){
            return RestResponse.fail("id不能为空");
        }
        return vipPrerogativeService.submit(id);
    }

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

    /**
     * 重写修改
     *
     * @param request
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @Override
    public RestResponse update(@RequestBody VipPrerogativeEntity request) {
        request.setExamineFlag(0);
        boolean updateFlag = getService().updateById(request);
        VipPrerogativeEntity e = getService().getById(request.getId());
        if (updateFlag) {
            return RestResponse.success(e, "修改成功");
        } else {
            return RestResponse.fail("修改失败");
        }
    }

    /**
     * 重写条件查询的方法
     * @param request
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("/query-page")
    @Override
    public RestResponse getCondition(@RequestBody VipPrerogativeQuery request) {
        if(request.getPageNum() < 1){
            return RestResponse.fail("error", "当前页不能小于1");
        }else{
            IPage<VipPrerogativeEntity> list = getService().page(request.page(), request.condition());
            //查询数据字典表的用户类型
            TableDictDto tableDictDto  = new TableDictDto();
            tableDictDto.setTableName("user_info");
            tableDictDto.setField("user_level");
            List<TableDictVo> constant = iTableDictService.getConstantByTableAndColumn(tableDictDto);
            //组装
            if(ListUtil.isNotEmpty(list.getRecords())){
                VipPrerogativeEntity entity = list.getRecords().get(0);
                entity.setUserTypeList(constant);
                return RestResponse.success(list, "查询成功");
            }else{
                return RestResponse.fail("暂无数据");
            }

        }

    }

}