package io.hmit.modules.serviceOrder.controller;

import io.hmit.annotation.Login;
import io.hmit.annotation.LoginUser;
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.entity.UserEntity;
import io.hmit.modules.serviceOrder.dto.PensionConsigneeAddDTO;
import io.hmit.modules.serviceOrder.dto.PensionConsigneeDTO;
import io.hmit.modules.serviceOrder.service.PensionConsigneeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

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


/**
 * 
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-05-06
 */
@Slf4j
@RestController
@RequestMapping("serviceOrder/pensionconsignee")
@Api(tags="用户地址")
public class PensionConsigneeController {
    @Autowired
    private PensionConsigneeService pensionConsigneeService;

    @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")
    })
    public Result<PageData<PensionConsigneeDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionConsigneeDTO> page = pensionConsigneeService.page(params);

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

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

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

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

        pensionConsigneeService.save(dto);

        return new Result();
    }

    @Deprecated
    @PutMapping
    @ApiOperation("修改")
    public Result update(@RequestBody PensionConsigneeDTO dto){
        //校验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        pensionConsigneeService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //校验数据
        AssertUtils.isArrayEmpty(ids, "id");
        //查询是否默认地址，如果删除的地址是默认地址，应将某个地址设为默认地址
        pensionConsigneeService.delete(ids);

        return new Result();
    }

    @Deprecated
    @GetMapping("findByUserId")
    @ApiOperation("根据用户id查询用户收货地址")
    public Result<List<PensionConsigneeDTO>> findAddressByUserId(@RequestParam("userId") Long userId){
        List<PensionConsigneeDTO> pensionConsigneeDTOList = pensionConsigneeService.findByUserId(userId);
        return new Result<List<PensionConsigneeDTO>>().ok(pensionConsigneeDTOList);
    }

    @Deprecated
    @Login
    @GetMapping("findMyAddress")
    @ApiOperation("查询本人收货地址")
    public Result<List<PensionConsigneeDTO>> findMyAddress(@ApiIgnore @LoginUser UserEntity user){
        List<PensionConsigneeDTO> pensionConsigneeDTOList = pensionConsigneeService.findByUserId(user.getId());
        return new Result<List<PensionConsigneeDTO>>().ok(pensionConsigneeDTOList);
    }

    @Login
    @GetMapping("findMyFamilyAddress")
    @ApiOperation("查询个人及家人收货地址")
    public Result<List<PensionConsigneeDTO>> findMyFamilyAddress(@ApiIgnore @LoginUser UserEntity user){
        List<PensionConsigneeDTO> pensionConsigneeDTOList = pensionConsigneeService.findMyFamilyAddress(user.getId());
        return new Result<List<PensionConsigneeDTO>>().ok(pensionConsigneeDTOList);
    }

    @Deprecated
    @Login
    @PostMapping("addMyAddress")
    @ApiOperation("添加我的收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户姓名", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "mobile", value = "用户手机号", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "province", value = "省份", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "city", value = "城市", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "district", value = "区县", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "address", value = "详细地址", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "zip", value = "邮编", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "ifDefault", value = "是否默认，传0则非默认，1则设为为默认，用户第一个地址传0或1均为即为默认地址", paramType = "query",required = true, dataType="int") ,
    })
    public Result addMyAddress(@ApiIgnore @LoginUser UserEntity user, @ApiIgnore @RequestParam Map<String, Object> params){
        params.put("userId", user.getId());
        pensionConsigneeService.addUserAddress(params);
        return new Result();
    }

    @Login
    @PostMapping("addMyAddressDTO2")
    @ApiOperation(value = "添加或修改我的收货地址",notes = "userId留空")
    public Result addMyAddress2(@ApiIgnore @LoginUser UserEntity user,
                               @RequestBody PensionConsigneeAddDTO dto){
        dto.setUserId(user.getId());
        pensionConsigneeService.addUserAddress(dto);
        return new Result();
    }

    @Deprecated
    @PostMapping("addUserAddress")
    @ApiOperation("根据用户Id添加收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query", required = true, dataType="long") ,
            @ApiImplicitParam(name = "username", value = "用户姓名", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "mobile", value = "用户手机号", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "province", value = "省份", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "city", value = "城市", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "district", value = "区县", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "address", value = "详细地址", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "zip", value = "邮编", paramType = "query",required = true, dataType="String") ,
            @ApiImplicitParam(name = "ifDefault", value = "是否默认，传0则非默认，1则设为为默认，用户第一个地址传0或1均为即为默认地址", paramType = "query",required = true, dataType="int") ,
    })
    public Result addUserAddress(@ApiIgnore @RequestParam Map<String, Object> params){
        pensionConsigneeService.addUserAddress(params);
        return new Result();
    }

}