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

import cn.com.ty.lift.common.verify.Val;
import cn.com.ty.lift.common.verify.Validation;
import cn.com.ty.lift.enterprise.custom.dao.entity.PropertyCompany;
import cn.com.ty.lift.enterprise.custom.dao.entity.model.PropertyCompanyAndPropertyContactReq;
import cn.com.ty.lift.enterprise.custom.dao.entity.model.PropertyCompanyReq;
import cn.com.ty.lift.enterprise.custom.service.impl.PropertyCompanyServiceImpl;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户接口(物业公司)(甲方公司) 前端控制器
 *
 * @author wang-hai-cheng
 * @since 2019/11/27
 */
@RestController
@RequestMapping("/property-company")
@AllArgsConstructor
public class PropertyCompanyController {
    private final PropertyCompanyServiceImpl propertyCompanyService;

    /**
     * 根据条件查询客户列表
     *
     * @param request 客户名称或客户地址或客户邮寄地址或客户联系方式,页码,页大小,维保公司id
     * @return RestResponse
     */
    @PostMapping("/list")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse propertyCompanies(@Val @RequestBody PropertyCompanyReq request) {
        return propertyCompanyService.propertyCompanies(request);
    }

    /**
     * 根据id获取客户详细信息
     *
     * @return RestResponse
     * @since 2019/11/27 11:36
     */
    @PostMapping("/detail")
    @Validation(fields = {"id"})
    public RestResponse propertyCompany(@Val @RequestBody PropertyCompanyReq request) {
        PropertyCompany byId = propertyCompanyService.getById(request.getId());
        return RestResponse.success(byId);
    }

    /**
     * 新增客户
     *
     * @param request [客户]
     * @return RestResponse
     */
    @PostMapping("/add")
    public RestResponse add(@RequestBody PropertyCompanyAndPropertyContactReq request) {
        return propertyCompanyService.saveAll(request);
    }

    /**
     * 更新客户信息
     *
     * @param propertyCompany [客户]
     * @return RestResponse
     */
    @PostMapping("/update")
    @Validation(fields = {"id"})
    public RestResponse update(@Val @RequestBody PropertyCompany propertyCompany) {
        boolean result = propertyCompanyService.updateById(propertyCompany);
        return RestResponse.success(result);
    }

    /**
     * 删除客户
     *
     * @param req [客户id]
     * @return cn.com.xwy.boot.web.dto.RestResponse
     */
    @PostMapping("/delete")
    @Validation(fields = {"id"})
    public RestResponse delete(@Val @RequestBody PropertyCompanyReq req) {
        return propertyCompanyService.delete(req.getId());
    }

    /**
     * 项目模块用，根据维保公司id查询客户公司列表
     *
     * @param req [mtCompanyId] 维保公司id
     * @return 客户公司列表
     */
    @PostMapping("/project/customs")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse customs(@Val @RequestBody PropertyCompanyReq req) {
        List<PropertyCompany> propertyCompanies = propertyCompanyService.list(new QueryWrapper<PropertyCompany>()
                .select("id", "name")
                .eq("mt_company_id", req.getMtCompanyId())
                .ne("status", 0));
        if (propertyCompanies.isEmpty()) {
            return RestResponse.success();
        }
        return RestResponse.success(propertyCompanies);
    }

    /**
     * 项目模块，新增客户
     *
     * @since 2019/12/16 17:08
     */
    @PostMapping("/project/custom/add")
    public RestResponse customAdd(@RequestBody PropertyCompany propertyCompany) {
        propertyCompany.setStatus(2);
        boolean result = propertyCompanyService.save(propertyCompany);
        if (result) {
            return RestResponse.success(propertyCompany.getId());
        }
        return RestResponse.fail();
    }

    @PostMapping("/export")
    @Validation(fields = {"mtCompanyId"})
    public void export(@Val @RequestBody PropertyCompanyReq req, HttpServletResponse response) {
        propertyCompanyService.export(req, response);
    }
}