package com.dayou.controller;

import com.dayou.annotation.DataPermission;
import com.dayou.annotation.OperLog;
import com.dayou.dto.CustomerDTO;
import com.dayou.utils.LoginContext;
import com.dayou.vo.VisitVO;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.controller.BaseController;
import com.dayou.service.IVisitService;
import com.dayou.entity.Visit;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.utils.ConvertUtil;
import com.dayou.utils.HttpKit;
import com.dayou.exception.ErrorCode;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import static com.dayou.common.Constants.MANAGER_OFFICE;

/**
 * 拜访记录表
 *
 * @author wucl
 * @since 2023-01-10
 */
@RestController
@RequestMapping("visit")
@Slf4j
public class VisitController extends BaseController {
    @Autowired
    private IVisitService visitService;

    /**
    * 拜访记录表列表
    */
    @DataPermission(teamData = true,postChild = false,ignoreDepartment = {MANAGER_OFFICE})
    @GetMapping("")
    public RestResponse<Page<VisitVO>> page(VisitVO visit, Page page,String keyword){
        Page<VisitVO> pages=visitService.selectPage(page,visit,keyword);
        return RestResponse.data(pages);
    }

    /**
     * 拜访记录表详情
     */
    @GetMapping("/{id}")
    public RestResponse<VisitVO> detail(@PathVariable Long id){
        VisitVO xVisit =visitService.detail(id);
        return RestResponse.data(xVisit);
     }

    /**
     * 拜访记录表新增
     */
    @OperLog(title = "新增拜访记录")
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody Visit visit) {
        visit.setUserId(LoginContext.getUserId());
        Boolean ret = visitService.add(visit);
        return RestResponse.data(ret);
    }

    /**
     * 拜访记录表更新
     */
    @OperLog(title = "更新拜访记录")
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody Visit visit) {
        Boolean ret = visitService.update(visit);
        return RestResponse.data(ret);
    }

    /**
     * 拜访记录表删除
     */
    @OperLog(title = "删除拜访记录")
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = visitService.delete(id);
        return RestResponse.data(ret);
    }

    /**
     * 拜访记录导出
     */
    @DataPermission(teamData = true,postChild = false,ignoreDepartment = {MANAGER_OFFICE})
    @GetMapping("/export")
    public void exportData(VisitVO visit,String keyword, HttpServletResponse response ) throws IOException {
        List<VisitVO> list = visitService.getList(visit,keyword);
        exportPlus(response,"拜访记录",list,VisitVO.class);
    }

}

