package com.huiju.foyo.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.foyo.common.BaseController;
import com.huiju.foyo.common.ConstantFoyo;
import com.huiju.foyo.common.ResponseBean;
import com.huiju.foyo.model.TdPartner;
import com.huiju.foyo.service.ITdPartnerService;
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.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 java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
    * 合作伙伴  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/")
public class TdPartnerController extends BaseController {

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

    @Autowired
    public ITdPartnerService iTdPartnerService;


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

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

    /**
     * 保存对象
     * @param tdPartner 实体对象
     * @return
     */
    @RequestMapping(value="/admin/partner/add",method= RequestMethod.POST)
    public ResponseBean tdPartnerAdd(@RequestBody TdPartner tdPartner){
        ResponseBean responseBean = new ResponseBean();
        try {
            tdPartner.setCreateBy(ConstantFoyo.USER_ADMIN);
            tdPartner.setStatus(ConstantFoyo.STATUS_EFFECTIVE);
            tdPartner.setCreateTime(LocalDateTime.now());
            if (iTdPartnerService.save(tdPartner)){
                responseBean.addSuccess(tdPartner);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPartnerAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id批量删除对象
     */
    @ResponseBody
    @RequestMapping(value="/admin/partner/delete", method= RequestMethod.DELETE)
    public ResponseBean tdPartnerDelete(@RequestBody String[] ids){
        ResponseBean responseBean = new ResponseBean();
        try {
            List<String> idList = Arrays.asList(ids);
            if(iTdPartnerService.removeByIds(idList)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPartnerDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/tdPartner/{id}",method= RequestMethod.GET)
    public ResponseBean tdPartnerGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTdPartnerService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPartnerDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/pc/partner/list",method= RequestMethod.GET)
    public ResponseBean pcPartnerList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TdPartner> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TdPartner> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",ConstantFoyo.STATUS_EFFECTIVE);
            queryWrapper.orderByDesc("create_time");
            
            IPage<TdPartner> result = iTdPartnerService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPartnerList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
