package io.hmit.modules.serviceOrder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.hmit.common.constant.Constant;
import io.hmit.common.page.PageData;
import io.hmit.common.utils.Result;
import io.hmit.common.validator.AssertUtils;
import io.hmit.common.validator.ValidatorUtils;
import io.hmit.common.validator.group.AddGroup;
import io.hmit.common.validator.group.DefaultGroup;
import io.hmit.common.validator.group.UpdateGroup;
import io.hmit.modules.serviceOrder.dto.PensionServiceDTO;
import io.hmit.modules.serviceOrder.dto.PensionServiceTypeDTO;
import io.hmit.modules.serviceOrder.service.PensionServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 服务表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-01-26
 */
@RestController
@RequestMapping("serviceOrder/pensionservice")
@Api(tags="服务表")
public class PensionServiceController {
    @Autowired
    private PensionServiceService pensionServiceService;

    private BaseMapper baseMapper;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "serviceTypeId", value = "服务大类ID", paramType = "query", dataType="int")
    })
    public Result<PageData<PensionServiceDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionServiceDTO> page = pensionServiceService.page(params);

        return new Result<PageData<PensionServiceDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<PensionServiceDTO> get(@PathVariable("id") Long id){
        PensionServiceDTO data = pensionServiceService.get(id);

        return new Result<PensionServiceDTO>().ok(data);
    }


    @GetMapping("getByServiceId/{serviceId}")
    @ApiOperation("根据服务获取详情页面")
    public Result<PensionServiceDTO> getByServiceId(@PathVariable("serviceId") Long serviceId){
        PensionServiceDTO data =  pensionServiceService.get(serviceId);

        return new Result<PensionServiceDTO>().ok(data);
    }

    @GetMapping("getSecondListByServiceId")
    @ApiOperation("根据服务id获取二级列表--可查询免费")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isFree", value = "是否免费：1 是， 2 否", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "pid", value = "服务大类ID", paramType = "query", dataType="int")
    })
    public Result<List<PensionServiceDTO>> getSecondListByServiceId(@ApiIgnore @RequestParam Map<String, Object> params){
        List<PensionServiceDTO> data =  pensionServiceService.list(params);

        return new Result<List<PensionServiceDTO>>().ok(data);
    }

    @GetMapping("getServiceList")
    @ApiOperation("老人/子女服务大类--包含免费")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isFree", value = "是否免费：1 是， 2 否", paramType = "query", dataType="String")
    })
    public Result<List<PensionServiceDTO>> getServiceList(@ApiIgnore @RequestParam Map<String, Object> params){
        List<PensionServiceDTO> data =  pensionServiceService.getFirstServiceList(params);

        return new Result<List<PensionServiceDTO>>().ok(data);
    }

/*
//V1 根据serviceType 获取服务大类
    @GetMapping("getByServiceTypeId/{serviceTypeId}")
    @ApiOperation("根据服务大类获取详情")
    public Result<PensionServiceDTO> getByServiceTypeId(@PathVariable("serviceTypeId") Long serviceTypeId){
        List<PensionServiceDTO> data =  baseMapper.selectList(
                new QueryWrapper<PensionServiceDTO>().lambda()
                        .eq(PensionServiceDTO::getServiceParentId,serviceTypeId));

        return new Result<PensionServiceDTO>().ok(data.get(0));
    }

*/
    @PostMapping
    @ApiOperation("保存")
    public Result save(@RequestBody PensionServiceDTO dto){
        //校验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        pensionServiceService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    //@RequiresPermissions("serviceOrder:pensionservice:update")
    public Result update(@RequestBody PensionServiceDTO dto){
        //校验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        pensionServiceService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")

    //@RequiresPermissions("serviceOrder:pensionservice:delete")
    public Result delete(@RequestBody Long[] ids){
        //校验数据
        AssertUtils.isArrayEmpty(ids, "id");

        pensionServiceService.delete(ids);

        return new Result();
    }
}
