package com.njzscloud.supervisory.biz.controller;

import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.biz.constant.AuditStatus;
import com.njzscloud.supervisory.biz.pojo.entity.BizCompanyEntity;
import com.njzscloud.supervisory.biz.pojo.param.ModifyBizCompanyParam;
import com.njzscloud.supervisory.biz.pojo.param.SearchCompanyParam;
import com.njzscloud.supervisory.biz.pojo.param.SettlementWayParam;
import com.njzscloud.supervisory.biz.pojo.param.SsrParam;
import com.njzscloud.supervisory.biz.pojo.result.SearchCompanyResult;
import com.njzscloud.supervisory.biz.pojo.result.StationResult;
import com.njzscloud.supervisory.biz.service.BizCompanyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业信息
 */
@Slf4j
@RestController
@RequestMapping("/biz_company")
@RequiredArgsConstructor
public class BizCompanyController {

    private final BizCompanyService bizCompanyService;

    /**
     * 新增站点
     */
    @PostMapping("/addStation")
    public R<?> add(@RequestBody BizCompanyEntity entity) {
        bizCompanyService.addStation(entity);
        return R.success();
    }

    /**
     * 修改
     *
     * @param modifyBizCompanyParam 数据
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody ModifyBizCompanyParam modifyBizCompanyParam) {
        bizCompanyService.modify(modifyBizCompanyParam);
        return R.success();
    }

    /**
     * 修改结算方式
     *
     * @param settlementWayParam 数据
     */
    @PostMapping("/updateSettlementWay")
    public R<?> updateSettlementWay(@RequestBody SettlementWayParam settlementWayParam) {
        BizCompanyEntity entity = new BizCompanyEntity();
        entity.setId(settlementWayParam.getId());
        entity.setSettlementWay(settlementWayParam.getSettlementWay());
        bizCompanyService.updateById(entity);
        return R.success();
    }

    /**
     * 修改服务范围和备注
     *
     * @param ssr 数据
     */
    @PostMapping("/updateSsr")
    public R<?> updateSsr(@RequestBody SsrParam ssr) {
        BizCompanyEntity entity = new BizCompanyEntity();
        entity.setId(ssr.getId());
        if (!ssr.getServiceScope().isEmpty()) {
            entity.setServiceScope(ssr.getServiceScope());
        }
        if (ssr.getRemark() != null) {
            entity.setRemark(ssr.getRemark());
        }
        bizCompanyService.updateById(entity);
        return R.success();
    }

    /**
     * 修改是否可选
     *
     * @param id Id
     */
    @GetMapping("/enabled")
    public R<?> enabled(@RequestParam("id") Long id, @RequestParam("enabled") Boolean enabled) {
        BizCompanyEntity entity = new BizCompanyEntity();
        entity.setId(id);
        entity.setEnabled(enabled);
        bizCompanyService.updateById(entity);
        return R.success();
    }

    /**
     * 撤销
     *
     * @param id Id
     */
    @GetMapping("/cancel")
    public R<?> cancel(@RequestParam Long id) {
        bizCompanyService.cancel(id);
        return R.success();
    }

    /**
     * 审核
     *
     * @param id Id
     */
    @GetMapping("/audit")
    public R<?> audit(@RequestParam Long id,
                      @RequestParam AuditStatus auditStatus,
                      @RequestParam(required = false, defaultValue = "") String auditMemo) {
        bizCompanyService.audit(id, auditStatus, auditMemo);
        return R.success();
    }

    /**
     * 详情
     *
     * @param id Id
     * @return BizCompanyEntity 结果
     */
    @GetMapping("/detail")
    public R<SearchCompanyResult> detail(@RequestParam(required = false) Long id) {
        return R.success(bizCompanyService.detail(id));
    }

    /**
     * 分页查询
     *
     * @param searchCompanyParam 筛选条件
     * @param pageParam          分页参数
     * @return PageResult&lt;BizCompanyEntity&gt; 分页结果
     */
    @GetMapping("/paging")
    public R<PageResult<BizCompanyEntity>> paging(PageParam pageParam, SearchCompanyParam searchCompanyParam) {
        return R.success(bizCompanyService.paging(pageParam, searchCompanyParam));
    }

    /**
     * 站点列表
     *
     * @param lng 经度
     * @param lat 纬度
     */
    @GetMapping("/station")
    public R<List<StationResult>> station(Double lng, Double lat) {
        return R.success(bizCompanyService.station(lng, lat));
    }

    /**
     * 禁启用
     *
     * @param id Id
     */
    @GetMapping("/open")
    public R<?> open(@RequestParam("id") Long id, @RequestParam("open") Boolean open) {
        bizCompanyService.open(id, open);
        return R.success();
    }

    /**
     * 是否启用
     *
     * @param id Id
     */
    @GetMapping("/is_open")
    public R<Boolean> isOpen(@RequestParam("id") Long id) {
        return R.success(bizCompanyService.isOpen(id));
    }
}
