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

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.base.entity.ParkingWay;
import com.jpsoft.shinestar.modules.base.service.ParkingWayService;
import com.jpsoft.shinestar.modules.sys.entity.User;
import com.jpsoft.shinestar.modules.sys.service.UserService;
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/parkingWay")
@Api(description = "parkingWay")
public class ParkingWayController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ParkingWayService parkingWayService;
    @Autowired
    private UserService userService;

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

        ParkingWay parkingWay = new ParkingWay();

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

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

        try {
            User user = userService.get(subject);

            parkingWay.setId(UUID.randomUUID().toString());
            parkingWay.setCompanyId(user.getCompanyId());
            parkingWay.setDelFlag(false);
            parkingWay.setCreateBy(subject);
            parkingWay.setCreateTime(new Date());

            int affectCount = parkingWayService.insert(parkingWay);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(parkingWay);
            } 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<ParkingWay> edit(@PathVariable("id") String id){
        MessageResult<ParkingWay> msgResult = new MessageResult<>();

        try {
            ParkingWay parkingWay = parkingWayService.get(id);

            if (parkingWay != null) {
                msgResult.setResult(true);
                msgResult.setData(parkingWay);
            } 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<ParkingWay> update(@RequestBody ParkingWay parkingWay,@RequestAttribute String subject){
        MessageResult<ParkingWay> msgResult = new MessageResult<>();

        try {
            parkingWay.setUpdateBy(subject);
            parkingWay.setUpdateTime(new Date());
            
            int affectCount = parkingWayService.update(parkingWay);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(parkingWay);
            } 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 {
            ParkingWay parkingWay = parkingWayService.get(id);
            parkingWay.setDelFlag(true);
            parkingWay.setUpdateBy(subject);
            parkingWay.setUpdateTime(new Date());

            int affectCount = parkingWayService.update(parkingWay);

            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) {
                ParkingWay parkingWay = parkingWayService.get(id);
                parkingWay.setDelFlag(true);
                parkingWay.setUpdateBy(subject);
                parkingWay.setUpdateTime(new Date());

                affectCount += parkingWayService.update(parkingWay);
            }

            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(
            String name,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize,
            @RequestAttribute String subject){

        //当前用户ID
        System.out.println(subject);
        User user = userService.get(subject);

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

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

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("sort_","asc"));

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

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

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

        return msgResult;
    }
}
