package com.huiju.estateagents.center.taUser.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.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.center.taUser.entity.TaUserRole;
import com.huiju.estateagents.center.taUser.service.ITaUserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
    * 用户角色表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-09-11
 */
@RestController
@RequestMapping("/")
public class TaUserRoleController extends BaseController {

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

    @Autowired
    public ITaUserRoleService iTaUserRoleService;


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

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

    /**
     * 保存对象
     * @param taUserRole 实体对象
     * @return
     */
    @RequestMapping(value="/center/taUserRole",method= RequestMethod.POST)
    public ResponseBean taUserRoleAdd(@RequestBody TaUserRole taUserRole){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaUserRoleService.save(taUserRole)){
                responseBean.addSuccess(taUserRole);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taUserRoleAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 批量保存用户角色
     * @param taUserRole 实体对象
     * @return
     */
    @RequestMapping(value="/center/taUserRoleList",method= RequestMethod.POST)
    public ResponseBean taUserRoleListAdd(@RequestBody TaUserRole taUserRole){
        ResponseBean responseBean = new ResponseBean();
        try {
            List<TaUserRole> taUserRoleList = new ArrayList<>();
            List<Integer> roleIdList = taUserRole.getRoleIdList();
            roleIdList.forEach(e -> {
                TaUserRole taUserRoles = new TaUserRole();
                taUserRoles.setRoleId(e);
                taUserRole.setUserId(taUserRole.getUserId());
                taUserRoleList.add(taUserRoles);
            });
            if (iTaUserRoleService.saveBatch(taUserRoleList)){
                responseBean.addSuccess(taUserRole);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taUserRoleAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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