package com.yunzhi.marketing.xlk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.xlk.entity.UserInstitution;
import com.yunzhi.marketing.xlk.service.IUserInstitutionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
    * 用户组织机构表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2021-07-23
 */
@RestController
@RequestMapping("/")
public class UserInstitutionController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(UserInstitutionController.class);

    @Autowired
    public IUserInstitutionService iUserInstitutionService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/userInstitution",method= RequestMethod.GET)
    public ResponseBean userInstitutionList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                            @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<UserInstitution> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<UserInstitution> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");

            IPage<UserInstitution> result = iUserInstitutionService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("userInstitutionList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param userInstitution 实体对象
     * @return
     */
    @RequestMapping(value="/userInstitution",method= RequestMethod.POST)
    public ResponseBean userInstitutionAdd(@RequestBody UserInstitution userInstitution){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iUserInstitutionService.save(userInstitution)){
                responseBean.addSuccess(userInstitution);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("userInstitutionAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/userInstitution/{id}", method= RequestMethod.DELETE)
    public ResponseBean userInstitutionDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iUserInstitutionService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("userInstitutionDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param userInstitution 实体对象
     * @return
     */
    @RequestMapping(value="/userInstitution/{id}",method= RequestMethod.PUT)
    public ResponseBean userInstitutionUpdate(@PathVariable Integer id,
                                        @RequestBody UserInstitution userInstitution){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iUserInstitutionService.updateById(userInstitution)){
                responseBean.addSuccess(userInstitution);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("userInstitutionUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/userInstitution/{id}",method= RequestMethod.GET)
    public ResponseBean userInstitutionGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iUserInstitutionService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("userInstitutionDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
