package com.ylx.web.controller.massage;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.controller.BaseController;
import com.ylx.common.core.domain.model.WxLoginUser;
import com.ylx.common.enums.BusinessType;
import com.ylx.massage.domain.BusinessDevelopment;
import com.ylx.massage.service.BusinessDevelopmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.ylx.common.core.domain.R;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 招商代理(BusinessDevelopment)表控制层
 *
 * @author makejava
 * @since 2024-07-29 16:42:47
 */
@RestController
@Api(tags = {"招商代理(BusinessDevelopment)表控制层"})
@RequestMapping("businessDevelopment")
public class BusinessDevelopmentController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private BusinessDevelopmentService businessDevelopmentService;

    /**
     * 分页查询所有数据
     *
     * @param page                分页对象
     * @param businessDevelopment 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    @ApiOperation("分页查询所有数据")
    @Log(title = "招商新增查看", businessType = BusinessType.OTHER)
    public R selectAll(Page<BusinessDevelopment> page, BusinessDevelopment businessDevelopment) {

        LambdaQueryWrapper<BusinessDevelopment> businessDevelopmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        businessDevelopmentLambdaQueryWrapper.like(StringUtils.isNotBlank(businessDevelopment.getCity()),BusinessDevelopment::getCity, businessDevelopment.getCity())
                .like(StringUtils.isNotBlank(businessDevelopment.getName()),BusinessDevelopment::getName, businessDevelopment.getName())
                .like(StringUtils.isNotBlank(businessDevelopment.getCompany()),BusinessDevelopment::getCompany, businessDevelopment.getCompany())
                .eq(StringUtils.isNotBlank(businessDevelopment.getPhoneNum()),BusinessDevelopment::getPhoneNum, businessDevelopment.getPhoneNum());
        return R.ok(this.businessDevelopmentService.page(page, businessDevelopmentLambdaQueryWrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.businessDevelopmentService.getById(id));
    }
    @GetMapping("getByOpenId")
    @ApiOperation("通过OpenId查询单条数据")
    public R<BusinessDevelopment> getByOpenId(String openId) {
        LambdaQueryWrapper<BusinessDevelopment> businessDevelopmentQueryWrapper = new LambdaQueryWrapper<>();
        businessDevelopmentQueryWrapper.eq(BusinessDevelopment::getOpenId,openId);
        return R.ok(this.businessDevelopmentService.getOne(businessDevelopmentQueryWrapper));
    }

    /**
     * 新增数据
     *
     * @param businessDevelopment 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    @ApiOperation("新增数据")
    @Log(title = "招商新增数据", businessType = BusinessType.INSERT)
    public R insert(@RequestBody BusinessDevelopment businessDevelopment) {

        WxLoginUser wxLoginUser = this.getWxLoginUser();

        LambdaQueryWrapper<BusinessDevelopment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BusinessDevelopment::getOpenId, wxLoginUser.getCOpenid());
        List<BusinessDevelopment> list = businessDevelopmentService.list(queryWrapper);
        if (!list.isEmpty()) {
            return R.fail("已提交过申请");
        }
        businessDevelopment.setOpenId(wxLoginUser.getCOpenid());
        return R.ok(this.businessDevelopmentService.save(businessDevelopment));
    }

    /**
     * 修改数据
     *
     * @param businessDevelopment 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    @ApiOperation("修改数据")
    @Log(title = "招商新增修改数据", businessType = BusinessType.UPDATE)
    public R update(@RequestBody BusinessDevelopment businessDevelopment) {
        return R.ok(this.businessDevelopmentService.updateById(businessDevelopment));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("delete")
    @ApiOperation("删除数据")
    @Log(title = "招商新增删除数据", businessType = BusinessType.UPDATE)
    public R delete(@RequestBody ArrayList<String> idList) {
        return R.ok(this.businessDevelopmentService.removeByIds(idList));
    }
}

