package com.sckw.system.controller;

import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.BaseResult;
import com.sckw.core.web.response.HttpResult;
import com.sckw.system.model.KwsEnterprise;
import com.sckw.system.model.vo.req.*;
import com.sckw.system.model.vo.res.EntBaseInfo;
import com.sckw.system.model.vo.res.EntInfo;
import com.sckw.system.model.vo.res.EntFindInfoResp;
import com.sckw.system.model.vo.res.EntInfoResp;
import com.sckw.system.model.vo.res.KwsUserSystemTypeVo;
import com.sckw.system.model.vo.res.PlatformEnterpriseResVo;
import com.sckw.system.service.KwsEnterpriseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 企业信息
 *
 * @author zk
 * @date 2023-05-31
 */
@RestController
@RequestMapping("/kwsEnt")
@Tag(name = "企业信息相关接口")
public class KwsEnterpriseController {

    @Autowired
    private KwsEnterpriseService kwsEntService;

    /**
     * @param id 主键ID
     * @return HttpResult
     * @description 根据主键查询
     * @author zk
     * @date 2023/06/02
     **/
    @GetMapping("/detail")
    public HttpResult detail(@RequestParam Long id,
                             @RequestParam(value = "isAll", required = false) Boolean isAll) {
        return HttpResult.ok(kwsEntService.detail(id, isAll));
    }

    /**
     * @param id 企业信息
     * @desc 企业联系人信息
     * @author zk
     * @date 2023/9/1
     **/
    @GetMapping("/detailOpen")
    public HttpResult detailOpen(@RequestParam Long id) {
        return HttpResult.ok(kwsEntService.detailOpen(id));
    }

    /**
     * 运营端 分页查询
     * 企业端 查询子公司
     * @param pageReqVo
     * @return HttpResult
     * @description 分页查询
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/findPage")
    public HttpResult findPage(@RequestBody EntFindPageReqVo pageReqVo) {
        return HttpResult.ok(kwsEntService.findPage(pageReqVo));
    }

    /**
     * @param pageReqVo
     * @return HttpResult
     * @description 分页查询
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/findListGroup")
    public HttpResult findListGroup(@RequestBody EntFindPageReqVo pageReqVo) {
        return HttpResult.ok(kwsEntService.findListGroup(pageReqVo));
    }


    /**
     * 企业用户绑定客户经理
     *
     * @param vo 请求参数
     * @return return
     */
    @RequestMapping(name = "企业用户绑定客户经理", value = "/enterpriseBindManager", method = RequestMethod.POST)
    public HttpResult enterpriseBindManager(@Valid @RequestBody EnterpriseBindManagerVo vo) {
        try {
            kwsEntService.enterpriseBindManage(vo);
        } catch (Exception e) {
            return HttpResult.error(e.getMessage());
        }
        return HttpResult.ok();
    }

    /**
     * 查询客户经理
     *
     * @return return
     */
    @RequestMapping(name = "查询客户经理", value = "/getManagerAll", method = RequestMethod.GET)
    public HttpResult getManagerAll() {
        List<KwsUserSystemTypeVo> managerAll = new ArrayList<>();
        try {
            managerAll = kwsEntService.getManagerAll();
        } catch (Exception e) {
            return HttpResult.error(e.getMessage());
        }
        return HttpResult.ok(managerAll);
    }

    /**
     * @param id 主键ID
     * @return HttpResult
     * @description 企业资质信息
     * @author zk
     * @date 2023/06/02
     **/
    @GetMapping("/certificate")
    public HttpResult certificate(@RequestParam Long id) {
        return HttpResult.ok(kwsEntService.certificate(id));
    }

    /**
     * @return HttpResult
     * @description 注册 企业认证
     * @author zk
     * @date 2023/06/02
     **/
    @PostMapping("/register")
    public HttpResult register(@Valid @RequestBody EntRegisterReqVo reqVo) {
        kwsEntService.register(reqVo);
        return HttpResult.ok(HttpStatus.MSG_009);
    }

    /**
     * 平台管理端，新增入住接口?
     * 企业端 新增子公司
     * @param reqVo 新增企业入驻入参
     * @return HttpResult
     * @desc: 新增企业入驻
     * @author: czh
     * @date: 2023/6/19
     */
    @PostMapping("/addEntSettle")
    public HttpResult addEntSettle(@Valid @RequestBody AddEntSettleReqVo reqVo, HttpServletRequest request) {
        kwsEntService.addEntSettle(reqVo);
        return HttpResult.ok(HttpStatus.MSG_003);
    }

    /**
     * @param params {name 机构名称、company 是否公司}
     * @return HttpResult
     * @description 新增
     * @author zk
     * @date 2023/06/02
     **/
    @PostMapping("/add")
    @Deprecated
    public HttpResult add(@RequestBody KwsEnterprise params) {
        kwsEntService.add(params);
        return HttpResult.ok(HttpStatus.MSG_003);
    }

    /**
     * @return HttpResult
     * @description 更新企业信息
     * @author zk
     * @date 2023/06/02
     **/
    @Deprecated
    @PostMapping("/update")
    public HttpResult update(@RequestBody UpdateEntInfoReqVo reqVo) {
        kwsEntService.update(reqVo);
        return HttpResult.ok(HttpStatus.MSG_005);
    }

    /**
     * @return HttpResult
     * @description 更新资质
     * @author zk
     * @date 2023/06/02
     **/
    @PostMapping("/updateCertificate")
    public HttpResult updateCertificate(@RequestBody UpdateCertificateReqVo reqVo) {
        kwsEntService.updateCertificate(reqVo);
        return HttpResult.ok(HttpStatus.MSG_005);
    }

    /**
     * @param reqVo {ids：主键ID（多个以逗号隔开）}
     * @return HttpResult
     * @description 删除
     * @author zk
     * @date 2023/06/02
     **/
    @PostMapping("/dels")
    public HttpResult del(@RequestBody IdReqVo reqVo) {
        kwsEntService.deleteByKey(reqVo.getIds());
        return HttpResult.ok();
    }

    /**
     * 平台运营端审核
     * @param {entId 企业id、status状态0正常1锁定、remark 审批意见}
     * @return HttpResult
     * @description 企业审批
     * @author zk
     * @date 2023/06/02
     **/
    @PostMapping(value = "/approval")
    public HttpResult approval(@Valid @RequestBody ApprovalReqVo reqVo) {
        kwsEntService.approval(reqVo);
        return HttpResult.ok(HttpStatus.MSG_004);
    }

    /**
     * @param entId 主键ID
     * @return HttpResult
     * @description 企业审批记录
     * @author zk
     * @date 2023/06/02
     **/
    @GetMapping("/entCheck")
    public HttpResult entCheck(@RequestParam Long entId) {
        return HttpResult.ok(kwsEntService.entCheck(entId));
    }

    /**
     * @param {entId 企业id、status 状态0正常1锁定}
     * @return HttpResult
     * @description 企业冻结/解冻
     * @author zk
     * @date 2023/06/02
     **/
    @PostMapping(value = "/freeze")
    public HttpResult freeze(@Valid @RequestBody FreezeReqVo reqVo) {
        kwsEntService.freeze(reqVo);
        return HttpResult.ok(HttpStatus.MSG_005);
    }

    /**
     * @param {entId 变更主账号企业ID, phone 替换账号，captcha 验证码}
     * @return HttpResult
     * @description 变更主账号（被替换账号冻结）
     * @author zk
     * @date 2023/06/02
     **/
    @PostMapping("/changeMasterAccount")
    public HttpResult changeMasterAccount(@RequestBody Map<String, String> params) {
        return null;
    }


    /**
     * @return HttpResult
     * @desc: 查企业下的员工
     * @param: reqVo 企业id
     * @author: czh
     * @date 2023/7/18
     */
    @PostMapping("findEntUser")
    public HttpResult findEntUser(@RequestBody FindListReqVo reqVo) {
        return HttpResult.ok(kwsEntService.findEntUser(reqVo));
    }

    /**
     * @return HttpResult
     * @desc: 查所有企业
     * @param: reqVo 企业id
     * @author: czh
     * @date 2023/7/18
     */
    @PostMapping("findList")
    public HttpResult findList(@RequestBody FindListReqVo reqVo) {
        return HttpResult.ok(kwsEntService.findList(reqVo));
    }

    /**
     * @param reqVo id
     * @return HttpResult
     * @desc: 查询企业的信息+主体机构信息
     * @author: czh
     * @date: 2023/7/22
     */
    @PostMapping("queryEntOrganization")
    public HttpResult queryEntOrganization(@RequestBody IdReqVo reqVo) {
        return HttpResult.ok(kwsEntService.queryCurrentOrganization(reqVo.getId()));
    }

    /**
     * @param reqVo id
     * @return HttpResult
     * @desc: 查询其他入驻企
     * @author: czh
     * @date: 2023/7/22
     */
    @PostMapping("queryOtherEnt")
    public HttpResult queryOtherEnt(@RequestBody IdReqVo reqVo, HttpServletRequest request) {
        return HttpResult.ok(kwsEntService.queryOtherEnt(reqVo.getId(), request));
    }

    /**
     * @param reqVo 查询
     * @return HttpResult
     * @desc: 客户列表分组个数
     * @author: czh
     * @date: 2023/9/14
     */
    @PostMapping("queryCustomerGroup")
    public HttpResult queryCustomer(@RequestBody EntFindPageReqVo reqVo) {
        return HttpResult.ok(kwsEntService.queryCustomerGroup(reqVo));
    }

    /**
     * @desc 企业联系人信息
     * @author zk
     * @date 2023/9/1
     **/
    @GetMapping("/findAllEnterprise")
    public HttpResult findAllEnterprise() {
        return kwsEntService.findAllEnterprise();
    }

    /**
     * @return 平台所有企业
     */
    @GetMapping("/findPlatformEnterprise")
    @Operation(summary = "查询平台所有企业")
    public BaseResult<List<PlatformEnterpriseResVo>> findPlatformEnterprise() {
        return BaseResult.success(kwsEntService.findPlatformEnterprise());
    }

    /**
     * @desc: 入驻认证/客户列表导出
     * @author: czh
     * @date: 2023/9/21
     */
    @PostMapping(value = "exportEntSettle", produces = MediaType.APPLICATION_JSON_VALUE)
    public void exportEntSettle(@RequestBody EntFindPageReqVo pageReqVo, HttpServletResponse response) {
        kwsEntService.exportEntSettle(pageReqVo, response);
    }

    /**
     * 查询供应商或者采购方的企业信息
     */
    @PostMapping("/queryEntInfo")
    @Operation(summary = "查询供应采购托运企业信息")
    public BaseResult<EntInfoResp> queryEntInfo(@RequestBody EntInfoReq req) {
        return BaseResult.success(kwsEntService.queryEntInfo(req));
    }

    /**
     * 查询供应商企业或采购企业
     */
    @PostMapping("/querySupOrProEnt")
    public BaseResult<EntFindInfoResp> querySupOrProEnt() {
        EntFindInfoResp entFindInfoRes = kwsEntService.querySupOrProEnt();
        return BaseResult.success(entFindInfoRes);
    }

    /**
     * 查询企业
     */
    @PostMapping("/queryKwsEnterprise")
    @Operation(summary = "查询企业信息")
    public BaseResult<EntInfoResp> queryKwsEnterprise(@RequestBody EntInfoReq req) {
        return BaseResult.success(kwsEntService.queryKwsEnterprise(req));
    }

    /**
     * 按页面类型查询企业列表
     */
    @PostMapping("/queryEnterpriseByPageType")
    @Operation(summary = "按企业类型查询企业列表")
    public BaseResult<List<EntInfo>> queryEnterpriseByPageType(@Valid @RequestBody EnterprisePageQueryReqVo req) {
        return BaseResult.success(kwsEntService.queryEnterpriseByPageType(req));
    }

    /**
     * 查询企业信息
     * @param entName
     * @return
     */
    @GetMapping("/query")
    @Operation(summary = "通过企业名称精确查询企业信息")
    public BaseResult<EntInfo> query(@RequestParam("entName") String entName) {
        return BaseResult.success(kwsEntService.queryEnt(entName));
    }

    /**
     * 查询企业及子企业数据
     * @return
     */
    @GetMapping("/child")
    public BaseResult<List<EntBaseInfo>> child() {
        return BaseResult.success(kwsEntService.childEnt());
    }
    /**
     * 查询所有母企业
     * @return
     */
    @GetMapping("/list")
    public BaseResult<List<EntBaseInfo>> list() {
        return BaseResult.success(kwsEntService.list());
    }

}
