package com.huiju.estateagents.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.*;
import com.huiju.estateagents.entity.TaOfficeContact;
import com.huiju.estateagents.service.ITaOfficeContactService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
    * 联系咨询  前端控制器
    * </p>
 *
 * @author yansen
 * @since 2020-06-11
 */
@RestController
@RequestMapping("/api")
public class TaOfficeContactController extends BaseController {

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

    private final static String SEQ_PREFIX = "office-web";

    @Autowired
    public ITaOfficeContactService iTaOfficeContactService;

    @Autowired
    public DBUtils dbUtils;

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/channel/taOfficeContact",method= RequestMethod.GET)
    public ResponseBean taOfficeContactList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                            @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                            @RequestParam(value ="company",required = false) String company,
                                            @RequestParam(value ="name",required = false) String name,
                                            @RequestParam(value ="phone",required = false) String phone,
                                            @RequestParam(value ="status",required = false) Integer status
                                            ){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaOfficeContact> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaOfficeContact> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(!StringUtils.isEmpty(company),"company",company);
            queryWrapper.like(!StringUtils.isEmpty(name),"name",name);
            queryWrapper.like(!StringUtils.isEmpty(phone),"phone",phone);
            queryWrapper.eq(null != status,"status",status);
            queryWrapper.gt("status", CommConstant.STATUS_DELETE);
            queryWrapper.orderByDesc("create_date");

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

    /**
     * 保存对象
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value="/official/taOfficeContact",method= RequestMethod.POST)
    public ResponseBean taOfficeContactAdd(@RequestBody TaOfficeContact taOfficeContact){
        ResponseBean responseBean = new ResponseBean();
        try {
            // 主键格式 20200611001  日期 + 数字,  数字 3 位, 前面补 0
            String today = DateUtils.todayCompact();
            Integer no = dbUtils.nextVal(SEQ_PREFIX + "-" + today);
            String serialNo = today + StringUtils.lpad(no.toString(), "0", 3);
            taOfficeContact.setSerialNo(serialNo);
            taOfficeContact.setCreateDate(LocalDateTime.now());
            taOfficeContact.setStatus(2);   // 待跟进
            if (iTaOfficeContactService.save(taOfficeContact)){
                responseBean.addSuccess(taOfficeContact);
                iTaOfficeContactService.sendNotice(taOfficeContact);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taOfficeContactAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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