package com.huiju.estateagents.center.taUser.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.TpLifeConsultantEvaluate;
import com.huiju.estateagents.center.taUser.service.ITpLifeConsultantEvaluateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
    * 生活顾问评价表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2020-12-17
 */
@RestController
@RequestMapping("/api")
@Api(value = "生活顾问评分 API", description = "生活顾问评分 API")
public class TpLifeConsultantEvaluateController extends BaseController {

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

    @Autowired
    public ITpLifeConsultantEvaluateService iTpLifeConsultantEvaluateService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/tpLifeConsultantEvaluate",method= RequestMethod.GET)
    public ResponseBean tpLifeConsultantEvaluateList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value ="userId") String userId,
                                                     @RequestParam(value ="evaluateTime", required = false) String evaluateTime,
                                                     HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TpLifeConsultantEvaluate> pg = new Page<>(pageNum, pageSize);
//            QueryWrapper<TpLifeConsultantEvaluate> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("org_id", getOrgId(request));
//            queryWrapper.eq("user_id", userId);
//            queryWrapper.eq("evaluate_time", evaluateTime);
//            queryWrapper.orderByDesc("create_date");
//
//            IPage<TpLifeConsultantEvaluate> result = iTpLifeConsultantEvaluateService.page(pg, queryWrapper);

            IPage<TpLifeConsultantEvaluate> result = iTpLifeConsultantEvaluateService.getPageBy(pg, getOrgId(request), userId, evaluateTime);

            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tpLifeConsultantEvaluateList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param tpLifeConsultantEvaluate 实体对象
     * @return
     */
    @ApiOperation(value = "生活顾问打分", notes = "生活顾问打分")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "TpLifeConsultantEvaluate",name = "tpLifeConsultantEvaluate",value = "")
    })
    @RequestMapping(value="/wx/tpLifeConsultantEvaluate",method= RequestMethod.POST)
    public ResponseBean tpLifeConsultantEvaluateAdd(@RequestBody TpLifeConsultantEvaluate tpLifeConsultantEvaluate, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            tpLifeConsultantEvaluate.setOrgId(getOrgId(request));
            tpLifeConsultantEvaluate.setPersonId(getPersonId(request));
            tpLifeConsultantEvaluate.setCreateDate(LocalDateTime.now());
            tpLifeConsultantEvaluate.setEvaluateTime(new SimpleDateFormat("yyyyMM").format(new Date()));
            LambdaQueryWrapper<TpLifeConsultantEvaluate> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TpLifeConsultantEvaluate::getOrgId,tpLifeConsultantEvaluate.getOrgId());
            queryWrapper.eq(TpLifeConsultantEvaluate::getPersonId,tpLifeConsultantEvaluate.getPersonId());
            queryWrapper.eq(TpLifeConsultantEvaluate::getEvaluateTime,tpLifeConsultantEvaluate.getEvaluateTime());
            queryWrapper.eq(TpLifeConsultantEvaluate::getUserId,tpLifeConsultantEvaluate.getUserId());
            List<TpLifeConsultantEvaluate> list = iTpLifeConsultantEvaluateService.list(queryWrapper);
            if (list.size() > 0){
                responseBean.addError("你当月已经投票了");
                return responseBean;
            }
            if (iTpLifeConsultantEvaluateService.save(tpLifeConsultantEvaluate)){
                responseBean.addSuccess(tpLifeConsultantEvaluate);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tpLifeConsultantEvaluateAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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