package com.njyunzhi.pet_identity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njyunzhi.pet_identity.common.*;
import com.njyunzhi.pet_identity.entity.TaPerson;
import com.njyunzhi.pet_identity.service.ITaSequenceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.njyunzhi.pet_identity.service.ITaPetIdentityService;
import com.njyunzhi.pet_identity.entity.TaPetIdentity;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
    * 宠物证件 前端控制器
    * </p>
 *
 * @author yansen
 * @since 2022-05-25
 */

@Api(tags = "宠物证件")
@RestController
@RequestMapping("/")
public class TaPetIdentityController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaPetIdentityController.class);

    @Autowired
    public ITaPetIdentityService iTaPetIdentityService;

    @Autowired
    public ITaSequenceService iTaSequenceService;

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/card",method= RequestMethod.GET)
    @ApiOperation(value="证件列表", notes = "证件列表", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean getList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                @ApiParam("卡号") @RequestParam(value ="cardNo", required = false) String cardNo,
                                @ApiParam("犬主") @RequestParam(value ="personName", required = false) String personName,
                                @ApiParam("手机号") @RequestParam(value ="phone", required = false) String phone,
                                @ApiParam("是否企业") @RequestParam(value ="isOrg", required = false) Boolean isOrg,
                                @ApiParam("企业名称") @RequestParam(value ="orgName", required = false) String orgName) throws Exception{

        IPage<TaPetIdentity> pg = new Page<>(pageNum, pageSize);
        QueryWrapper<TaPetIdentity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(cardNo), "card_no", "%" + cardNo + "%");
        queryWrapper.like(!StringUtils.isEmpty(personName), "person_name", "%" + personName + "%");
        queryWrapper.like(!StringUtils.isEmpty(phone), "phone", "%" + phone + "%");
        queryWrapper.eq(null != isOrg, "is_org", isOrg);
        queryWrapper.like(!StringUtils.isEmpty(orgName), "org_name", "%" + orgName + "%");
        queryWrapper.gt("status", Constants.STATUS_DELETE);
        queryWrapper.orderByDesc("create_date");

        IPage<TaPetIdentity> result = iTaPetIdentityService.page(pg, queryWrapper);
        return ResponseBean.success(result);
    }

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/wx/{clientId}/card",method= RequestMethod.GET)
    @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean wxList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
									 @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
        TaPerson taPerson = currentPerson();

        IPage<TaPetIdentity> pg = new Page<>(pageNum, pageSize);
        QueryWrapper<TaPetIdentity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("person_id", taPerson.getPersonId());
        queryWrapper.gt("status", Constants.STATUS_DELETE);
        queryWrapper.orderByDesc("create_date");

        IPage<TaPetIdentity> result = iTaPetIdentityService.page(pg, queryWrapper);
        return ResponseBean.success(result);
    }


    /**
     * 获取证件号码
     * @param id  实体ID
     */
    @RequestMapping(value="/admin/card/next",method= RequestMethod.GET)
    @ApiOperation(value="获取证件号码", notes = "获取证件号码", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean getNewNo() throws Exception{
        String seqName = String.format("%s-%s", "card", DateUtils.today("yyyyMMdd"));
        Integer nextVal = iTaSequenceService.getNextVal(seqName);

        String cardNo = StringUtils.lpad(nextVal.toString(), "0", 3);

        return ResponseBean.success(DateUtils.today("yyyyMMdd")+cardNo);
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/wx/{clientId}/card/{id}",method= RequestMethod.GET)
    @ApiOperation(value="证件详情", notes = "证件详情", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean taPetIdentityGet(@ApiParam("证件号码") @PathVariable String id) throws Exception{
        TaPetIdentity taPetIdentity = iTaPetIdentityService.getExistBy("card_no", id, false, true);
        if (null == taPetIdentity) {
            return ResponseBean.error("未找到证件信息");
        }

        return ResponseBean.success(taPetIdentity);
    }


    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/admin/card/{id}",method= RequestMethod.GET)
    @ApiOperation(value="证件详情", notes = "证件详情", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean getDetail(@ApiParam("对象ID") @PathVariable String id) throws Exception{
        TaPetIdentity taPetIdentity = iTaPetIdentityService.getExistBy("card_id", id, false, true);
        if (null == taPetIdentity) {
            return ResponseBean.error("未找到证件信息");
        }

        return ResponseBean.success(taPetIdentity);
    }
}
