package com.dayou.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dayou.annotation.DataPermission;
import com.dayou.annotation.OperLog;
import com.dayou.annotation.Transfer;
import com.dayou.dto.CustomerDTO;
import com.dayou.enums.BusinessResourceEnum;
import com.dayou.enums.OperationTypeEnum;
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.ICustomerService;
import com.dayou.entity.Customer;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
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("customer")
@Slf4j
public class CustomerController extends BaseController {
    @Autowired
    private ICustomerService customerService;

    /**
    * 客户表列表
    */
    @DataPermission(teamData = true,postChild = false,ignoreDepartment = {MANAGER_OFFICE})
    @GetMapping("")
    public RestResponse<Page<CustomerDTO>> page(CustomerDTO customer, Page page,String keyword){
        Page<CustomerDTO> pages=customerService.selectPage(page,customer,keyword);
        return RestResponse.data(pages);
    }

    /**
     * 客户表详情
     */
    @GetMapping("/{id}")
    public RestResponse<CustomerDTO> detail(@PathVariable Long id){
        CustomerDTO xCustomer =customerService.detail(id);
        return RestResponse.data(xCustomer);
     }

    /**
     * 客户表新增
     */
    @OperLog(title = "新增客户")
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody CustomerDTO customer) {
        Boolean ret = customerService.add(customer);
        return RestResponse.data(ret);
    }

    /**
     * 客户表更新
     */
    @OperLog(title = "更新客户")
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody CustomerDTO customer) {
        Boolean ret = customerService.update(customer);
        return RestResponse.data(ret);
    }

    /**
     * 客户表删除
     */
    @OperLog(title = "删除客户")
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = customerService.delete(id);
        return RestResponse.data(ret);
    }


    /**
     * 客户表下拉列表
     */
    @DataPermission
    @GetMapping("/simpleAll")
    public RestResponse<List<PullDownModel>> simpleAll(CustomerDTO customerDTO){
        LambdaQueryWrapper<Customer> lambdaQueryWrapper = new LambdaQueryWrapper<Customer>()
        .select(Customer::getId,Customer::getName)
        .eq(Customer::getDeleted,false)
        .in(CollectionUtils.isNotEmpty(customerDTO.getUserIds()),Customer::getUserId,customerDTO.getUserIds());
        List<Customer> list= customerService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(list, PullDownModel.class);
        return RestResponse.data(ret);
    }

    /**
     * 客户信息模板下载
     */
    @OperLog(title = "客户信息模板下载",businessType = OperationTypeEnum.EXPORT)
    @GetMapping(value = "/download/importTemplate")
    public void importTemplate() {
        try {
            XSSFWorkbook wb = customerService.generateImportTemplate();

            String fileName = "客户信息导入模板" + System.currentTimeMillis() + ".xlsx";

            writeXss2Response(HttpKit.getRequest(), HttpKit.getResponse(), wb, fileName);
        } catch (Exception e) {
            log.error("生成客户信息导入模板异常", e);
            ErrorCode.throwBusinessException(ErrorCode.DEFAULTERROR);
        }
    }

    /**
     * 客户信息导入
     */
    @OperLog(title = "客户信息导入",businessType = OperationTypeEnum.IMPORT)
    @PostMapping("/importExcel")
    public RestResponse<Boolean> importExcel(@RequestPart("file") MultipartFile file) {
        customerService.importExcel(file);
        return RestResponse.data(true);
    }

    /**
     * 客户表导出
     */
    @DataPermission
    @GetMapping("/export")
    public void exportData(CustomerDTO customer,HttpServletResponse response ) throws IOException {
        List<CustomerDTO> list = customerService.getList(customer);
        exportPlus(response,"客户列表",list,CustomerDTO.class);
    }
    @Transfer(resourceEnum = BusinessResourceEnum.CUSTOMER)
    @OperLog(title = "更新客户经理")
    @PutMapping("/changeManager")
    public RestResponse<Boolean> changeManager(@RequestBody CustomerDTO customer){
        Boolean result = customerService.changeManager(customer);
        return RestResponse.data(result);
    }
}

