package com.cool.bang.modules.business.controller;

import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.web.request.PageDomain;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.common.web.response.module.ResultTable;
import com.cool.bang.modules.business.domain.JobCustomer;
import com.cool.bang.modules.business.domain.LpbHouse;
import com.cool.bang.modules.business.service.IJobCustomerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 客源
 *
 * @author Sy
 * @version 1.0.0
 * 2024-10-21 18:05
 */
@RestController
@RequestMapping(ControllerConstant.BUSINESS + "customer/v1")
public class JobCustomerController extends BaseController {

    private IJobCustomerService jobCustomerService;

    @Autowired
    public JobCustomerController(IJobCustomerService jobCustomerService) {
        this.jobCustomerService = jobCustomerService;
    }

    @GetMapping
    @PreAuthorize("hasPermission('','customer:customer:main')")
    public ResultTable main(PageDomain pageDomain, JobCustomer param) {
        PageInfo<JobCustomer> jobCustomerPage = jobCustomerService.page(pageDomain, param);
        return pageTable(jobCustomerPage.getList(), jobCustomerPage.getTotal());
    }

    @GetMapping("{id}")
    @PreAuthorize("hasPermission('','customer:customer:main')")
    public Result get(@PathVariable Long id) {
        return success(jobCustomerService.queryById(id));
    }


    /**
     * 通过主键查询下所有收藏
     *
     * @param customerId 客源主键
     * @return RestResponse
     */
    @GetMapping("/houseList")
    public ResultTable getList(PageDomain pageDomain,String customerId) {
        PageInfo<LpbHouse>  list = jobCustomerService.queryByLpbHouse(pageDomain,Integer.valueOf(customerId));
        return pageTable(list.getList(), list.getTotal());
    }

    /**
     * 通过主键查询下所有预约咨询
     *
     * @param customerId 客源主键
     * @return RestResponse
     */
    @GetMapping("/consultList")
    public ResultTable getLConsultList(PageDomain pageDomain,String customerId) {
        PageInfo<JobCustomer>  list = jobCustomerService.queryByConsultList(pageDomain,Integer.valueOf(customerId));
        return pageTable(list.getList(), list.getTotal());
    }

    @PostMapping
    public Result add(@Validated JobCustomer jobCustomer) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String code = dtf.format(LocalDateTime.now());
        jobCustomer.setCustomerNumber(code);
        jobCustomer.insertConfigure();
        return decide(jobCustomerService.save(jobCustomer));
    }

    @PostMapping("edit")
    public Result edit(@Validated JobCustomer jobCustomer) {
        return decide(jobCustomerService.update(jobCustomer));
    }

    @DeleteMapping("{id}")
    public Result remove(@PathVariable Integer id) {
        return decide(jobCustomerService.removeById(id));
    }

}
