package com.huiju.estateagents.eContract.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.common.CommConstant;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.eContract.entity.TaContractTemplate;
import com.huiju.estateagents.eContract.service.ITaContractTemplateService;
import org.apache.commons.lang3.StringUtils;
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.List;

/**
 * <p>
    * 合同模板  前端控制器
    * </p>
 *
 * @author fxf
 * @since 2020-03-26
 */
@RestController
@RequestMapping("/api")
public class TaContractTemplateController extends BaseController {

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

    @Autowired
    public ITaContractTemplateService iTaContractTemplateService;


    /**
     * 分页查询列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/taContractTemplate",method= RequestMethod.GET)
    public ResponseBean taContractTemplateList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                               @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                               @RequestParam(value ="contractTemplateName", required = false) String contractTemplateName,
                                               HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaContractTemplate> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaContractTemplate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id", getOrgId(request));
            queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
            queryWrapper.like(!StringUtils.isEmpty(contractTemplateName),"contract_template_name", contractTemplateName);
            queryWrapper.orderByDesc("create_date");

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

    /**
     * 保存对象
     *
     * @param taContractTemplate 实体对象
     * @return
     */
    @RequestMapping(value = "/admin/contract/template/add", method = RequestMethod.POST)
    public ResponseBean taContractTemplateAdd(@RequestBody TaContractTemplate taContractTemplate,HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            taContractTemplate.setCreateDate(LocalDateTime.now());
            taContractTemplate.setStatus(CommConstant.STATUS_NORMAL);
            taContractTemplate.setOrgId(getOrgId(request));
            if (iTaContractTemplateService.save(taContractTemplate)) {
                responseBean.addSuccess(taContractTemplate);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taContractTemplateAdd -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 批量删除合同模板
     *
     * @param taContractTemplateList
     */
    @ResponseBody
    @RequestMapping(value = "/admin/contract/template/batchDelete", method = RequestMethod.PUT)
    public ResponseBean batchDeleteContractTemplate(@RequestBody List<TaContractTemplate> taContractTemplateList) {
        ResponseBean responseBean = new ResponseBean();
        try {
            taContractTemplateList.stream().forEach(record -> {
                record.setStatus(CommConstant.STATUS_DELETE);
            });
            responseBean.addSuccess(iTaContractTemplateService.updateBatchById(taContractTemplateList));
        } catch (Exception e) {
            logger.error("batchDeleteContractTemplate -=- {}", e);
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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