package com.cpop.oam.business.controller.backstage;

import com.alibaba.excel.EasyExcel;
import com.cpop.core.base.R;
import com.cpop.core.utils.SpringUtils;
import com.cpop.oam.business.bo.BusinessDisposeBo;
import com.cpop.oam.business.bo.BusinessDistributeBo;
import com.cpop.oam.business.bo.BusinessInfoPageBo;
import com.cpop.oam.business.bo.BusinessRemoveBo;
import com.cpop.oam.business.dto.BusinessDistributeDto;
import com.cpop.oam.business.service.BusinessService;
import com.cpop.oam.business.service.StaffService;
import com.cpop.oam.business.vo.*;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import static com.cpop.core.base.table.table.SysUserTableDef.SYS_USER;
import static com.cpop.oam.business.entity.table.StaffTableDef.STAFF;

/**
 * Oam-事务表 控制层。
 *
 * @author DB
 * @since 2023-12-12
 */
@RestController
@Api(tags = "市场模块")
@RequestMapping("/backstage/business")
public class BackstageBusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 获取事务分页列表
     * @author DB
     * @since 2023/12/13
     * @param status 状态（0:进行中;1:已完成）
     * @return R<Page<BusinessPageVo>>
     */
    @GetMapping("/getBusinessPage")
    @ApiOperation("获取事务分页列表")
    public R<Page<BusinessPageVo>> getBusinessPage(@ApiParam("状态（0:进行中;1:已完成）") Integer status) {
        Page<BusinessPageVo> page = businessService.getBusinessPage(status);
        return R.ok(page);
    }

    /**
     * 获取机构对接xml模板
     * @author DB
     * @since 2023/12/13
     * @return R<Void>
     */
    @GetMapping("/getBusinessTemplate")
    @ApiOperation("获取机构对接xml模板")
    public void getBusinessTemplate(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
        String fileName = URLEncoder.encode("事务分发模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), BusinessDistributeDto.class).sheet("模板").doWrite(new ArrayList<>());
    }

    /**
     * 事务分发
     * @author DB
     * @since 2023/12/13
     * @param bo 请求数据
     * @return R<Void>
     */
    @PostMapping("/businessDistribute")
    @ApiOperation("事务分发")
    public R<Void> businessDistribute(@RequestBody @ApiParam("请求参数") @Validated BusinessDistributeBo bo) {
        businessService.businessDistribute(bo);
        return R.ok();
    }

    /**
     * 获取市场部员工
     * @author DB
     * @since 2023/12/13
     * @return R<List<StaffVo>>
     */
    @GetMapping("/getBusinessStaff")
    @ApiOperation("获取市场部员工")
    public R<List<StaffVo>> getBusinessStaff() {
        List<StaffVo> list = SpringUtils.getBean(StaffService.class).queryChain()
                .leftJoin(SYS_USER).on(SYS_USER.ID.eq(STAFF.USER_ID))
                .where(STAFF.STAFF_TYPE.eq(1))
                .and(SYS_USER.STATUS.eq(1))
                .listAs(StaffVo.class);
        return R.ok(list);
    }

    /**
     * 获取个人事务分页列表/管理人员事务详情列表
     * @author DB
     * @since 2023/12/13
     * @param bo 请求参数
     * @return R<Page<BusinessInfoPageVo>>
     */
    @PostMapping("/getBusinessInfoPage")
    @ApiOperation("获取个人事务分页列表/管理人员事务详情列表")
    public R<Page<BusinessInfoPageVo>> getBusinessInfoPage(@ApiParam("请求参数") @RequestBody @Validated BusinessInfoPageBo bo) {
        Page<BusinessInfoPageVo> page = businessService.getBusinessInfoPage(bo);
        return R.ok(page);
    }

    /**
     * 获取个人当前事务详情
     * @author DB
     * @since 2023/12/29
     * @param id 事务详情id
     * @return R<PersonBusinessInfoVo>
     */
    @GetMapping("/getPersonBusinessInfoById/{id}")
    @ApiOperation("获取个人当前事务详情")
    public R<PersonBusinessInfoVo> getPersonBusinessInfoById(@ApiParam("请求参数") @PathVariable String id) {
        PersonBusinessInfoVo vo = businessService.getPersonBusinessInfoById(id);
        return R.ok(vo);
    }

    /**
     * 员工事务处理
     * @author DB
     * @since 2023/12/13
     * @param bo 请求
     * @return R<Void>
     */
    @PutMapping("/businessDispose")
    @ApiOperation("员工事务处理")
    public R<Void> businessDistribute(@RequestBody @ApiParam("请求参数") @Validated BusinessDisposeBo bo) {
        businessService.businessDispose(bo);
        return R.ok();
    }

    /**
     * 事务详情删除
     * @author DB
     * @since 2023/12/14
     * @return R<Void>
     */
    @DeleteMapping("/businessRemove")
    @ApiOperation("事务详情删除")
    public R<Void> businessRemove(@Validated @RequestBody BusinessRemoveBo bo) {
        businessService.businessRemove(bo);
        return R.ok();
    }

    /**
     * 查询未签约校区分页列表
     * @author DB
     * @since 2024/1/22
     * @param params 请求参数
     * @return R<Page<StorePoolPageVo>>
     */
    @ApiOperation("查询未签约校区分页列表")
    @GetMapping("/getUnSignStorePage")
    public R<Page<BusinessUnSignPageVo>> getUnSignStorePage(@ApiParam("查询参数") @RequestParam(value = "params", required = false) String params) {
        Page<BusinessUnSignPageVo> pageVo = businessService.getUnSignStorePage(params);
        return R.ok(pageVo);
    }

    /**
     * 校区签约
     * @author DB
     * @since 2024/1/22
     * @param id 校区id
     * @return R<Void>
     */
    @ApiOperation("校区签约")
    @PutMapping("/storeSign/{id}")
    public R<Void> storeSign(@ApiParam(value = "校区id", required = true) @PathVariable String id) {
        businessService.storeSign(id);
        return R.ok();
    }

}
