package cn.com.ty.lift.enterprise.custom.controller;

import cn.com.ty.lift.enterprise.custom.dao.entity.PropertyContact;
import cn.com.ty.lift.enterprise.custom.dao.entity.model.PropertyContactReq;
import cn.com.ty.lift.enterprise.custom.service.impl.PropertyCompanyServiceImpl;
import cn.com.ty.lift.enterprise.custom.service.impl.PropertyContactServiceImpl;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (甲方联系人)客户联系人表 前端控制器
 *
 * @author wang-hai-cheng
 * @since 2019/11/27
 */
@RestController
@RequestMapping("/property-contact")
@AllArgsConstructor
public class PropertyContactController {
    private final PropertyCompanyServiceImpl propertyCompanyService;
    private final PropertyContactServiceImpl propertyContactService;

    /**
     * 根据客户id分页查它的联系人
     *
     * @param propertyContact [ppCompanyId, pageNum, pageSize]
     * @return cn.com.xwy.boot.web.dto.RestResponse
     * @since 2019/11/29 8:21
     */
    @PostMapping("/list")
    public RestResponse propertyContacts(@RequestBody PropertyContactReq propertyContact) {
        if (propertyCompanyService.getById(propertyContact.getPpCompanyId()) == null) {
            return RestResponse.success();
        }
        Page<PropertyContact> page = new Page<>(propertyContact.getPageNum(), propertyContact.getPageSize());
        IPage<PropertyContact> records = propertyContactService.page(page, new QueryWrapper<PropertyContact>()
                .select("id", "pp_company_id", "name", "job", "telephone", "status")
                .eq("pp_company_id", propertyContact.getPpCompanyId())
                .ne("status", 0)
        );
        if (records.getRecords().isEmpty()) {
            return RestResponse.success();
        }
        return RestResponse.success(records, "成功");
    }

    /**
     * 根据客户id，新增联系人
     *
     * @param propertyContact [propertyContact]
     * @return cn.com.xwy.boot.web.dto.RestResponse
     * @since 2019/11/27 11:23
     */
    @PostMapping("/add")
    public RestResponse add(@RequestBody PropertyContact propertyContact) {
        if (propertyCompanyService.getById(propertyContact.getPpCompanyId()) == null) {
            return RestResponse.success();
        }
        if (propertyContactService.save(propertyContact)) {
            return RestResponse.success(propertyContact.getId(), "成功");
        }
        return RestResponse.fail();
    }

    /**
     * 编辑联系人
     *
     * @param propertyContact [propertyContact]
     * @return cn.com.xwy.boot.web.dto.RestResponse
     * @since 2019/11/27 11:26
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody PropertyContact propertyContact) {
        if (propertyContactService.updateById(propertyContact)) {
            return RestResponse.success(null, "成功");
        }
        return RestResponse.fail();
    }

    /**
     * 删除联系人
     *
     * @param propertyContact [id]
     * @return cn.com.xwy.boot.web.dto.RestResponse
     * @since 2019/11/27 11:28
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody PropertyContact propertyContact) {
        return propertyContactService.delete(propertyContact.getId());
    }

    @PostMapping("/project/contacts")
    public RestResponse customs(@RequestBody PropertyContact propertyContact) {
        List<PropertyContact> propertyCompanies = propertyContactService.list(new QueryWrapper<PropertyContact>()
                .select("id", "name", "telephone")
                .eq("pp_company_id", propertyContact.getPpCompanyId())
                .ne("status", 0)
        );
        if (propertyCompanies.isEmpty()) {
            return RestResponse.success();
        }
        return RestResponse.success(propertyCompanies);
    }

    /**
     * 项目模块，新增客户联系人
     *
     * @since 2019/12/16 17:09
     */
    @PostMapping("/project/contact/add")
    public RestResponse customAdd(@RequestBody PropertyContact propertyContact) {
        propertyContact.setStatus(2);
        if (propertyContactService.save(propertyContact)) {
            return RestResponse.success(propertyContact.getId(), "成功");
        }
        return RestResponse.fail();
    }
}