package com.yunzhi.marketing.xlk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.TaPolicyType;
import com.yunzhi.marketing.xlk.service.ITaPolicyTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
    * 购房政策类型表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2021-06-16
 */
@RestController
@RequestMapping("/api")
@Api(value = "购房政策类型表", tags = "xlk-购房政策类型表")
public class TaPolicyTypeController extends BaseController {

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

    @Autowired
    public ITaPolicyTypeService iTaPolicyTypeService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "admin-购房政策类型列表", notes = "admin-购房政策类型列表")
    @RequestMapping(value="/{plat}/taPolicyType",method= RequestMethod.GET)
    public ResponseBean taPolicyTypeList(@PathVariable String plat,
                                         @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                         @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                         @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaPolicyType> pg = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<TaPolicyType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TaPolicyType::getOrgId,getOrgId(request));
            queryWrapper.orderByDesc(TaPolicyType::getSort);

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

    /**
     * 保存对象
     * @param taPolicyType 实体对象
     * @return
     */
    @ApiOperation(value = "admin-购房政策类型新增", notes = "admin-购房政策类型新增")
    @RequestMapping(value="/admin/taPolicyType",method= RequestMethod.POST)
    public ResponseBean taPolicyTypeAdd(@RequestBody TaPolicyType taPolicyType,
                                        @RequestHeader("authorization") String token,
                                        HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            taPolicyType.setOrgId(getOrgId(request));
            taPolicyType.setCreateDate(LocalDateTime.now());
            if (iTaPolicyTypeService.save(taPolicyType)){
                responseBean.addSuccess(taPolicyType);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taPolicyTypeAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ApiOperation(value = "admin-购房政策类型删除", notes = "admin-购房政策类型删除")
    @ResponseBody
    @RequestMapping(value="/admin/taPolicyType/{id}", method= RequestMethod.DELETE)
    public ResponseBean taPolicyTypeDelete(@PathVariable String id,
                                           @RequestHeader("authorization") String token,
                                           HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iTaPolicyTypeService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taPolicyTypeDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param taPolicyType 实体对象
     * @return
     */
    @ApiOperation(value = "admin-购房政策类型修改", notes = "admin-购房政策类型修改")
    @RequestMapping(value="/admin/taPolicyType/{id}",method= RequestMethod.PUT)
    public ResponseBean taPolicyTypeUpdate(@PathVariable String id,
                                           @RequestBody TaPolicyType taPolicyType,
                                           @RequestHeader("authorization") String token,
                                           HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaPolicyTypeService.updateById(taPolicyType)){
                responseBean.addSuccess(taPolicyType);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taPolicyTypeUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @ApiOperation(value = "admin-购房政策类型详情", notes = "admin-购房政策类型详情")
    @RequestMapping(value="/admin/taPolicyType/{id}",method= RequestMethod.GET)
    public ResponseBean taPolicyTypeGet(@PathVariable String id,
                                        @RequestHeader("authorization") String token,
                                        HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaPolicyTypeService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taPolicyTypeDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
