package com.jpsoft.shinestar.modules.base.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.base.entity.ParkingMerchantBinder;
import com.jpsoft.shinestar.modules.base.service.ParkingMerchantBinderService;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/base/parkingMerchantBinder")
@Api(description = "商户绑定")
public class ParkingMerchantBinderController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ParkingMerchantBinderService parkingMerchantBinderService;

    @ApiOperation(value="创建空记录")
    @GetMapping("create")
    public MessageResult<ParkingMerchantBinder> create(){
        MessageResult<ParkingMerchantBinder> msgResult = new MessageResult<>();

        ParkingMerchantBinder parkingMerchantBinder = new ParkingMerchantBinder();

        msgResult.setData(parkingMerchantBinder);
        msgResult.setResult(true);

        return msgResult;
    }
    
    @ApiOperation(value="添加信息")
    @PostMapping("add")
    public MessageResult<ParkingMerchantBinder> add(@RequestBody ParkingMerchantBinder parkingMerchantBinder,@RequestAttribute String subject){
        MessageResult<ParkingMerchantBinder> msgResult = new MessageResult<>();

        try {
            parkingMerchantBinder.setId(UUID.randomUUID().toString());
            parkingMerchantBinder.setDelFlag(false);
            parkingMerchantBinder.setCreateBy(subject);
            parkingMerchantBinder.setCreateTime(new Date());
            
            int affectCount = parkingMerchantBinderService.insert(parkingMerchantBinder);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(parkingMerchantBinder);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="获取信息")
    @GetMapping("edit/{id}")
    public MessageResult<ParkingMerchantBinder> edit(@PathVariable("id") String id){
        MessageResult<ParkingMerchantBinder> msgResult = new MessageResult<>();

        try {
            ParkingMerchantBinder parkingMerchantBinder = parkingMerchantBinderService.get(id);

            if (parkingMerchantBinder != null) {
                msgResult.setResult(true);
                msgResult.setData(parkingMerchantBinder);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库不存在该记录!");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="更新用户")
    @PostMapping("update")
    public MessageResult<ParkingMerchantBinder> update(@RequestBody ParkingMerchantBinder parkingMerchantBinder,@RequestAttribute String subject){
        MessageResult<ParkingMerchantBinder> msgResult = new MessageResult<>();

        try {
            parkingMerchantBinder.setUpdateBy(subject);
            parkingMerchantBinder.setUpdateTime(new Date());
            
            int affectCount = parkingMerchantBinderService.update(parkingMerchantBinder);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(parkingMerchantBinder);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库更新失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

	@ApiOperation(value="删除")
    @PostMapping("delete/{id}")
    public MessageResult<Integer> delete(@PathVariable("id") String id,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            ParkingMerchantBinder parkingMerchantBinder = parkingMerchantBinderService.get(id);
            parkingMerchantBinder.setDelFlag(true);
            parkingMerchantBinder.setUpdateBy(subject);
            parkingMerchantBinder.setUpdateTime(new Date());

            int affectCount = parkingMerchantBinderService.update(parkingMerchantBinder);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }


    @ApiOperation(value="批量删除")
    @PostMapping("batchDelete")
    public MessageResult<Integer> batchDelete(@RequestBody List<String> idList,@RequestAttribute String subject){
        MessageResult<Integer> msgResult = new MessageResult<>();

        try {
            int affectCount = 0;

            for (String id : idList) {
                ParkingMerchantBinder parkingMerchantBinder = parkingMerchantBinderService.get(id);
                parkingMerchantBinder.setDelFlag(true);
                parkingMerchantBinder.setUpdateBy(subject);
                parkingMerchantBinder.setUpdateTime(new Date());

                affectCount += parkingMerchantBinderService.update(parkingMerchantBinder);
            }

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(affectCount);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("删除失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="列表")
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    public MessageResult<Map> pageList(
            @RequestParam(value="name",defaultValue="") String name,
            @RequestParam(value="nickName",defaultValue="") String nickName,
            @RequestParam(value="merchantId",defaultValue="") String merchantId,
            @RequestParam(value="phone",defaultValue="") String phone,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            @RequestAttribute String subject){

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("a.create_time","desc"));

        if (StringUtils.isNotEmpty(name)) {
            searchParams.put("name","%" + name + "%");
        }

        if (StringUtils.isNotEmpty(nickName)) {
            searchParams.put("nickName","%" + nickName + "%");
        }

        if (StringUtils.isNotEmpty(phone)) {
            searchParams.put("phone","%" + phone + "%");
        }

        if (StringUtils.isNotEmpty(merchantId)) {
            searchParams.put("parkMerchantId",merchantId);
        }

        Page<ParkingMerchantBinder> page = parkingMerchantBinderService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(page));

        return msgResult;
    }
}
