package com.huiju.estateagents.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.entity.TaBuildingReport;
import com.huiju.estateagents.service.ITaBuildingReportService;
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.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
    * 项目报表设置  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-08-08
 */
@RestController
@RequestMapping("/api")
public class TaBuildingReportController extends BaseController {

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

    @Autowired
    public ITaBuildingReportService iTaBuildingReportService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/taBuildingReports",method= RequestMethod.GET)
    public ResponseBean taBuildingReportList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                             @RequestParam(value ="pageSize",defaultValue = "20") Integer pageSize,
                                             HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaBuildingReport> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaBuildingReport> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");
            queryWrapper.eq("org_id", getOrgId(request));

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

    /**
     * 保存对象
     * @param jsonString
     * @return
     */
    @RequestMapping(value="/admin/taBuildingReport/add",method= RequestMethod.POST)
    public ResponseBean taBuildingReportAdd(@RequestBody String jsonString, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        Integer orgId = getOrgId(request);
        try {
            JSONArray jsonArray = JSONObject.parseObject(jsonString).getJSONArray("list");
            List<TaBuildingReport> list = new ArrayList<>();
            jsonArray.stream().forEach(e -> {
                TaBuildingReport taBuildingReport = new TaBuildingReport();
                taBuildingReport.setOrgId(orgId);
                taBuildingReport.setReportCode(e.toString());
                taBuildingReport.setCreateDate(LocalDateTime.now());
                list.add(taBuildingReport);
            });
            //先删除在添加
            iTaBuildingReportService.deleteAll(orgId);
            if (iTaBuildingReportService.saveBatch(list)){
                responseBean.addSuccess("保存成功");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            logger.error("taBuildingReportAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

    /**
     * 修改对象
     * @param id  实体ID
     * @param taBuildingReport 实体对象
     * @return
     */
    @RequestMapping(value="/taBuildingReport/{id}",method= RequestMethod.PUT)
    public ResponseBean taBuildingReportUpdate(@PathVariable Integer id,
                                        @RequestBody TaBuildingReport taBuildingReport,
                                               HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            taBuildingReport.setOrgId(getOrgId(request));
            if (iTaBuildingReportService.updateById(taBuildingReport)){
                responseBean.addSuccess(taBuildingReport);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            logger.error("taBuildingReportUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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