package com.ylx.web.controller.massage;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.common.exception.ServiceException;
import com.ylx.massage.domain.THighlights;
import com.ylx.massage.domain.TJs;
import com.ylx.massage.domain.TJsDay;
import com.ylx.massage.domain.TXiangmu;
import com.ylx.massage.domain.vo.TXiangmuDetailVo;
import com.ylx.massage.mapper.TJsDayMapper;
import com.ylx.massage.service.THighlightsService;
import com.ylx.massage.service.TJsService;
import com.ylx.massage.service.TXiangmuService;
import com.ylx.massage.utils.DateTimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目管理Controller
 *
 * @author ylx
 * @date 2024-03-22
 */
@RestController
@RequestMapping("api/xiangmu/v1")
@Slf4j
@Api(tags = {"项目管理"})
public class TXiangmuController {
    @Resource
    private TXiangmuService xiangmuService;


    @Resource
    private THighlightsService highlightsService;

    @Resource
    private TJsDayMapper jsDayMapper;

    @Resource
    private TJsService jsService;

    /**
     * 获取所有项目
     *
     * @param tXiangmu
     * @return R<List<TXiangmu>>
     */
    @RequestMapping(value = "wx/getAll", method = RequestMethod.POST)
    @ApiOperation("获取所有项目")
    public R<List<TXiangmu>> geTXiangmu(@RequestBody TXiangmu tXiangmu) {
        LambdaQueryWrapper<TXiangmu> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(StringUtils.isNotBlank(tXiangmu.getcId()), TXiangmu::getcId, tXiangmu.getcId());
        if (StringUtils.isNotBlank(tXiangmu.getcLdList())) {
            objectLambdaQueryWrapper.apply("FIND_IN_SET({0}, c_ld_list)", tXiangmu.getcLdList());
        }
        objectLambdaQueryWrapper.orderByAsc(TXiangmu::getdPrice);
        List<TXiangmu> list = xiangmuService.list(objectLambdaQueryWrapper);
        return R.ok(list);
    }


    /**
     * 查询指定技师的项目（H5端）
     *
     * @param openId
     * @return R<List<TXiangmu>>
     */
    @GetMapping(value = "wx/getByJsId")
    @ApiOperation("查询指定技师的项目")
    public R<List<TXiangmu>> getByJsId(@RequestParam String openId) {
        try {
            log.info("查询指定技师的项目，openId：{}", openId);
            //通过openId查询技师信息
            TJs js = jsService.getOne(new LambdaQueryWrapper<TJs>().eq(TJs::getcOpenId, openId));
            if (js == null) {
                return R.fail("该技师不存在");
            }
            String bhList = js.getcBhList();
            if (StringUtils.isBlank(bhList)) {
                return R.fail("该技师没有项目");
            }
            LambdaQueryWrapper<TXiangmu> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            objectLambdaQueryWrapper.in(TXiangmu::getcId, bhList.split(","));
            List<TXiangmu> list = xiangmuService.list(objectLambdaQueryWrapper);
            return R.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    /**
     * 推荐技师
     *
     * @param deptName
     * @return R<List<TJs>>
     */
    @RequestMapping(value = "wx/recommend", method = RequestMethod.GET)
    @ApiOperation("推荐技师")
    public R<List<TJs>> recommend(@RequestParam String deptName) {
//        //推荐技师
//        List<TJsDay> tJsDays = jsDayMapper.selectRanking(deptName, null,
//                DateTimeUtils.formatDate(DateTimeUtils.addMonths(new Date(), -1)), DateTimeUtils.formatDate(new Date()));
//        //查技师
//        if (CollectionUtil.isEmpty(tJsDays)) {
//            return null;
//        }
//        List<String> jsId = tJsDays.stream().map(TJsDay::getJsId).collect(Collectors.toList());

        LambdaQueryWrapper<TJs> jsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        jsLambdaQueryWrapper.eq(TJs::getnB1, 1).last("limit 4");
        return R.ok(jsService.list(jsLambdaQueryWrapper));
    }

    /**
     * 添加或者更新项目
     *
     * @param xiangmu
     * @return R
     */
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation("添加或者更新项目")
    public R saveOrUpdate(@RequestBody TXiangmu xiangmu) {
        try {
            return R.ok(xiangmuService.addOrUpdate(xiangmu));
        } catch (ServiceException s) {
            log.error(s.toString());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            log.error(e.toString());
            return R.fail("系统异常");
        }
    }


    /**
     * 分页查询项目数据
     * @param page
     * @param tXiangmu
     * @return R<Page<TXiangmu>>
     */
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @ApiOperation("分页查询项目数据")
    public R<Page<TXiangmu>> selecTXiangmu(Page<TXiangmu> page, TXiangmu tXiangmu) {
        LambdaQueryWrapper<TXiangmu> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.like(StringUtils.isNotBlank(tXiangmu.getcTitle()), TXiangmu::getcTitle, tXiangmu.getcTitle()).
                orderByDesc(TXiangmu::getDtCreateTime);
        // 获取查询返回结果
        Page<TXiangmu> pageSelect = xiangmuService.page(page, objectLambdaQueryWrapper);
        return R.ok(pageSelect);
    }


    /**
     * 分页查询数据
     */
    @ApiOperation("根据id查询项目")
    @RequestMapping(value = "/getByid", method = RequestMethod.POST)
    public R getById(@RequestBody TXiangmu xiangmu) {
        TXiangmuDetailVo details = xiangmuService.details(xiangmu);
        return R.ok(details);
    }

    /**
     * 删除项目
     *
     * @param xiangmu
     * @return
     */
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ApiOperation("删除项目")
    public R del(@RequestBody TXiangmu xiangmu) {
        return R.ok(xiangmuService.removeById(xiangmu));
    }

    /**
     * 项目亮点
     *
     * @return R<List<THighlights>>
     */
    @ApiOperation("项目亮点")
    @Log(title = "项目亮点", businessType = BusinessType.OTHER)
    @RequestMapping(value = "/highlights", method = RequestMethod.GET)
    public R<List<THighlights>> getHighlights() {
        return R.ok(highlightsService.list());
    }

}
